[英]getting this error: 'NoneType' object has no attribute 'find_all'
I am trying to get cuisines of new york city from the website: https://en.wikipedia.org/wiki/Cuisine_of_New_York_City我正在尝试从以下网站获取纽约市的美食: https : //en.wikipedia.org/wiki/Cuisine_of_New_York_City
and I am getting the error: 'NoneType' object has no attribute 'find_all'我收到错误:'NoneType' 对象没有属性 'find_all'
This is the code I have tried.这是我试过的代码。
website_url = requests.get('https://en.wikipedia.org/wiki/Cuisine_of_New_York_City').text
soup = BeautifulSoup(website_url,'lxml')
table = soup.find('table',{'class':'wikitable sortable'})
headers = [header.text for header in table.find_all('th')]
table_rows = table.find_all('tr')
rows = []
for row in table_rows:
td = row.find_all('td')
row = [row.text for row in td]
rows.append(row)
with open('BON2_POPULATION1.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(row for row in rows if row)
table
tag with class:wikitable sortable
property and so its returning noneclass:wikitable sortable
属性的table
标签,因此您收到此错误,因此它返回nonewebsite_url = requests.get('https://en.wikipedia.org/wiki/Cuisine_of_New_York_City').text
soup = BeautifulSoup(website_url,'lxml')
table = soup.find('table',{'class':'wikitable sortable'})
headers = [header.text for header in table.find_all('th')]
if table is None:
#handle something here when table is not present in your html.
else:
table_rows = table.find_all('tr')
rows = []
for row in table_rows:
td = row.find_all('td')
row = [row.text for row in td]
rows.append(row)
with open('BON2_POPULATION1.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(row for row in rows if row)
I see no element with that description.我看不到该描述的元素。 As a starters for 10 you could use
:contains
, with bs4 4.7.1+, and capture the elements with class mw-headline
that contains the word cuisine in their innerText/text
.作为 10 的初学者,您可以使用
:contains
和 bs4 4.7.1+,并使用类mw-headline
捕获元素,该类在其innerText/text
中包含单词 kitchen 。 The list is needs a little cleaning.该列表需要稍微清理一下。 If you intended something more specific then more info is needed in the question.
如果您想要更具体的内容,则问题中需要更多信息。
import requests
from bs4 import BeautifulSoup as bs
r = requests.get('https://en.wikipedia.org/wiki/Cuisine_of_New_York_City')
soup = bs(r.content, 'lxml')
cuisines_dirty = [i.text for i in soup.select('.mw-headline:contains(cuisine)')]
#perform some sort of cleaning on list
Dirty list:脏清单:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.