我试图从美国内核俱乐部(https://www.akc.org/reg/dogreg_stats.cfm)搜集数据,但遇到了一些麻烦。我指的是this stackoverflow post,我可以得到第二个表上的所有行,但我不能格式化它们。你知道吗
这是我的密码。你知道吗
from bs4 import BeautifulSoup
import requests
url = https://www.akc.org/reg/dogreg_stats.cfm
r. requests.get(r)
data= r.text
soup = BeautifulSoup(data)
rows = soup.find_all('table')[1].find_all('tr')
for row in rows:
cells = soup.find_all('td')
firstRanking = cell[1].get_text()
print(firstRanking)
打印出来的是
More on Registration Trends:
More on Registration Trends:
More on Registration Trends:
More on Registration Trends:
More on Registration Trends:
More on Registration Trends:
More on Registration Trends:
而不是实际排名。你知道吗
创建变量“cells”时,您希望查找该行的所有“
td
”元素,而不是整个“soup”对象的“”。你知道吗应该是这样的:
另外,我认为这之后的行中有一个错误,引用的是“cells”而不是“cell”:
这将使for循环如下所示:
我犯的主要错误是在这行行=汤。全部找到('table')[1].find_all('tr')<;-这创建了一个列表项。 为了解决这个问题,我将行改为table=汤。全部找到('table')[1]然后行=表.find\u all('tr')
相关问题 更多 >
编程相关推荐