【问题标题】:Cant use findAll() function无法使用 findAll() 函数
【发布时间】:2021-11-09 13:34:02
【问题描述】:

谁能告诉我为什么会出错

html_content = get_html_content(test)
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html_content, 'html.parser')
    productDetail = []
    product = soup.findAll("div",{"class":"s-result-item"})
    for pd in product:
        product = pd.find("div",class_="s-impression-counter")
        product_name = product.find('span',class_="a-text-normal").text
        productDetail.append(product_name)
        print(productDetail)

出现'NoneType' object has no attribute 'find'

我知道问题出在product 变量中,但如果我不允许使用findAll(),我应该使用什么?

【问题讨论】:

    标签: python django web-scraping beautifulsoup findall


    【解决方案1】:

    您正在分配 product 变量后直接在 for 循环中分配它。

    附带说明,您应该使用更新后的 find_all() 方法 - 这完全一样,但符合 Python style guide

    试试这个:-

    from bs4 import BeautifulSoup
    
    html_content = get_html_content(test)
    soup = BeautifulSoup(html_content, 'html.parser')
    productDetail = []
    all_products = soup.find_all("div",{"class":"s-result-item"})
    
    for pd in all_products:
        product = pd.find("div",class_="s-impression-counter")
        product_name = product.find('span',class_="a-text-normal").text 
        productDetail.append(product_name)
        print(productDetail)
    

    请注意,我已将您原来的 product 变量更改为 all_products,以确保变量之间存在区别。

    【讨论】:

    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 2013-06-12
    • 2023-03-06
    • 2013-09-13
    • 2020-04-08
    • 2017-04-24
    • 2019-11-17
    • 2021-11-27
    相关资源
    最近更新 更多