BeautifulSoup - AttributeError: 'NoneType' object has no attribute 'findAll'
BeautifulSoup - AttributeError: 'NoneType' object has no attribute 'findAll'
我在使用 .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' object has no attribute 'findAll'
查看您要抓取的页面的 HTML,很明显没有元素具有 id="row"
;因此,您会收到无法在 None
.
上调用 findAll
的错误
您可以跳过该步骤,直接查找具有 class_='well result-box nomargin'
:
的 'div'
(而不是 'section'
)元素
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
元素。
我在使用 .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' object has no attribute 'findAll'
查看您要抓取的页面的 HTML,很明显没有元素具有 id="row"
;因此,您会收到无法在 None
.
findAll
的错误
您可以跳过该步骤,直接查找具有 class_='well result-box nomargin'
:
'div'
(而不是 'section'
)元素
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
元素。