【发布时间】:2021-12-27 22:33:42
【问题描述】:
url = "https://www.imdb.com/chart/top/"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
r = requests.get(url,headers=headers)
soup = BeautifulSoup(r.content,"html.parser")
puan = soup.find_all("tr")
for i in puan:
puan2 = i.find_all("td",{"class":"ratingColumn"})
for x in puan2:
puan3 = x.find("strong")
print(puan3.text)
我正在使用 BeautifulSoup。在我找到的结果中,我收到一个错误,因为列表中有 NoneType。如何从列表中删除 NoneType 部分
【问题讨论】:
-
当
find_all返回无时,这意味着它没有找到您要查找的任何内容。具体来说,您的某些puan2集合中没有<strong>标记。如果您在内部循环中添加了print(x),您会看到。 -
问题已解决,非常感谢
标签: python beautifulsoup nonetype