AttributeError: ResultSet object has no attribute 'find_all'
AttributeError: ResultSet object has no attribute 'find_all'
我正在尝试复制一个网络抓取代码(来自教育网站)以从维基百科获取印度各州的名称。我一直收到 "AttributeError: ResultSet object has no attribute 'find_all'" 错误,因此无法继续。
我在这里添加我的代码,希望能找到一些指导和帮助:
#
#import library to query a website
from urllib.request import urlopen
#the url is stored in a variable called wiki
wiki="https://en.wikipedia.org/wiki/List_of_state_and_union_territory_capitals_in_India"
#open the html page and store it in a variable called page
page=urlopen(wiki)
print(wiki)
#import the Beautiful soup functions to parse the data returned from the website
from bs4 import BeautifulSoup
#parsing the html page stored in variable page and storing it in beautiful soup #format
soup=BeautifulSoup(page,['lxml','xml'])
print(soup)
#using 'prettify' function to structure the html page
print(soup.prettify())
#storing all the links in a variable
all_links=soup.find_all("a")
for link in all_links:
print(link.get("href"))
#storing all the tables in a variable
all_tables=soup.find_all("table")
print(all_tables)
#storing required data in a variable
right_table=soup.find_all('table',{'class':'wikitable sortable plainrowheaders'})
print(right_table)
#Generating lists
A=[]
B=[]
C=[]
D=[]
E=[]
F=[]
G=[]
for row in right_table.find_all("tr"):
cells = row.find_all('td')
states=row.find_all('th') #To store second column data
if len(cells)==6: #Only extract table body not heading
A.append(cells[0].find(text=True))
B.append(states[0].find(text=True))
C.append(cells[1].find(text=True))
D.append(cells[2].find(text=True))
E.append(cells[3].find(text=True))
F.append(cells[4].find(text=True))
G.append(cells[5].find(text=True))
我正在使用 python 3.6 和 Windows 10
在此先感谢您的帮助!
你打电话时我能看到的是:
right_table=soup.find_all('table',{'class':'wikitable sortable plainrowheaders'})
此命令的输出,即 right_table 包含一个数组。您需要在其成员上调用 find_all(即使它只有一个元素),而不是在整个成员上调用。
所以这一行:
for row in right_table.find_all("tr"):
应改为:
for row in right_table[0].find_all("tr"):
我正在尝试复制一个网络抓取代码(来自教育网站)以从维基百科获取印度各州的名称。我一直收到 "AttributeError: ResultSet object has no attribute 'find_all'" 错误,因此无法继续。 我在这里添加我的代码,希望能找到一些指导和帮助:
##import library to query a website
from urllib.request import urlopen
#the url is stored in a variable called wiki
wiki="https://en.wikipedia.org/wiki/List_of_state_and_union_territory_capitals_in_India"
#open the html page and store it in a variable called page
page=urlopen(wiki)
print(wiki)
#import the Beautiful soup functions to parse the data returned from the website
from bs4 import BeautifulSoup
#parsing the html page stored in variable page and storing it in beautiful soup #format
soup=BeautifulSoup(page,['lxml','xml'])
print(soup)
#using 'prettify' function to structure the html page
print(soup.prettify())
#storing all the links in a variable
all_links=soup.find_all("a")
for link in all_links:
print(link.get("href"))
#storing all the tables in a variable
all_tables=soup.find_all("table")
print(all_tables)
#storing required data in a variable
right_table=soup.find_all('table',{'class':'wikitable sortable plainrowheaders'})
print(right_table)
#Generating lists
A=[]
B=[]
C=[]
D=[]
E=[]
F=[]
G=[]
for row in right_table.find_all("tr"):
cells = row.find_all('td')
states=row.find_all('th') #To store second column data
if len(cells)==6: #Only extract table body not heading
A.append(cells[0].find(text=True))
B.append(states[0].find(text=True))
C.append(cells[1].find(text=True))
D.append(cells[2].find(text=True))
E.append(cells[3].find(text=True))
F.append(cells[4].find(text=True))
G.append(cells[5].find(text=True))
我正在使用 python 3.6 和 Windows 10
在此先感谢您的帮助!
你打电话时我能看到的是:
right_table=soup.find_all('table',{'class':'wikitable sortable plainrowheaders'})
此命令的输出,即 right_table 包含一个数组。您需要在其成员上调用 find_all(即使它只有一个元素),而不是在整个成员上调用。
所以这一行:
for row in right_table.find_all("tr"):
应改为:
for row in right_table[0].find_all("tr"):