BeautifulSoup-AttributeError:“ NoneType”对象没有属性“ findAll”

时间:2020-04-08 04:36:59

标签: python web-scraping beautifulsoup attributes

我在使用.findAll属性时遇到问题。当我运行下面的代码时,它说对象没有属性findAll

import requests
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings()

url = 'https://csgostash.com/weapon/Butterfly+Knife'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

results = soup.find(id="row")

job_elems = results.findAll('section', class_="well result-box nomargin")
for job_elem in job_elems:
    title_elem = jobelem.find('h3', class_='a')
    print(title_elem.text.strip())

错误消息

job_elems = results.findAll('section',class _ =“ well result-box nomargin”) AttributeError:“ NoneType”对象没有属性“ findAll”

1 个答案:

答案 0 :(得分:1)

在查看您要抓取的页面的HTML时,显然没有元素具有id="row";因此,您会收到无法在findAll上调用None的错误。

您可以跳过该步骤,直接使用'div'查找'section'(而不是class_='well result-box nomargin')元素:

job_elems = soup.find_all('div', class_="well result-box nomargin")             
for job_elem in job_elems:                                                      
    title_elem = job_elem.find('a')                                             
    if title_elem:                                                              
        print(title_elem.text.strip())

输出:

★ (Vanilla)
Marble Fade
Doppler
Tiger Tooth
Damascus Steel
Ultraviolet
Rust Coat
Fade
Slaughter
Crimson Web
Night
Case Hardened
Blue Steel
Boreal Forest
Stained
Forest DDPAT
Urban Masked
Scorched
Safari Mesh

请注意,您的代码中存在多个错误,包括jobelem而不是job_elem。尽管实际上您正在寻找嵌套在其中的h3元素,但您也尝试获取a元素的文本。