简体   繁体   English

不能使用 findAll() 函数

[英]Cant use findAll() function

can any body tell me why is error任何人都可以告诉我为什么是错误

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)

it appears 'NoneType' object has no attribute 'find'似乎'NoneType' object has no attribute 'find'

i know the problem is in the product variable, but if i not allow to use findAll() , what should i use ?我知道问题出在product变量中,但是如果我不允许使用findAll() ,我应该使用什么?

You are assigning the product variable in your for loop directly after its assignment.您在分配后直接在for循环中分配product变量。

On a side note, you should be using the updated find_all() method - this is exactly the same but it conforms to the Python style guide .附带说明一下,您应该使用更新的find_all()方法 - 这完全相同,但它符合Python 风格指南

Try this instead: -试试这个: -

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)

Note I have changed your original product variable to all_products to ensure there is a distinction between the variables.注意我已将您的原始product变量更改为all_products以确保变量之间存在区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM