简体   繁体   English

收到此错误:“NoneType”对象没有属性“find_all”

[英]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)
  • You are getting this error because of your soup.find not able to find any table tag with class:wikitable sortable property and so its returning none由于您的soup.find 无法找到任何带有class:wikitable sortable属性的table标签,因此您收到此错误,因此它返回none
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')]
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.

相关问题 Python属性错误:“ NoneType”对象没有属性“ find_all” - Python Attribute Error: 'NoneType' object has no attribute 'find_all' 属性错误“NoneType”对象没有属性“find_all” - attribute error 'NoneType' object has no attribute 'find_all' 错误:“。NoneType”对象没有.find的属性“ find_all” - Error: 'NoneType' object has no attribute 'find_all' for .find 'NoneType' object 没有属性 'find_all' - 'NoneType' object has no attribute 'find_all' Python错误:“ NoneType”对象没有属性“ find_all” - Python error: 'NoneType' object has no attribute 'find_all' NoneType' 对象没有属性 'find_all' 错误即将到来 - NoneType' object has no attribute 'find_all' error coming Beautiful Soup 错误:“NoneType”对象没有属性“find_all” - Beautiful Soup error: 'NoneType' object has no attribute 'find_all' 错误:“NoneType”object 没有属性“find_all” - ERROR: 'NoneType' object has no attribute 'find_all' 尝试使用以下代码进行抓取时,出现以下错误“AttributeError: 'NoneType' object has no attribute 'find_all'” - Getting the following error "AttributeError: 'NoneType' object has no attribute 'find_all'" when trying to scrape with below code 在编译像这样的“NoneType”对象没有属性“find_all”的代码时出现错误 - In am getting error while compiling code like this 'NoneType' object has no attribute 'find_all'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM