如何用靓汤从AKC狗狗注册网站上刮取数据?

2024-04-25 04:36:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从美国内核俱乐部(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:

而不是实际排名。你知道吗


Tags: httpsorgonmorewwwstatsregistrationall
2条回答

创建变量“cells”时,您希望查找该行的所有“td”元素,而不是整个“soup”对象的“”。你知道吗

应该是这样的:

cells = row.find_all('td')

另外,我认为这之后的行中有一个错误,引用的是“cells”而不是“cell”:

firstRanking = cells[1].get_text()

这将使for循环如下所示:

for row in rows:
  cells = row.find_all('td')
  firstRanking = cells[1].get_text()
  print(firstRanking)

我犯的主要错误是在这行行=汤。全部找到('table')[1].find_all('tr')<;-这创建了一个列表项。 为了解决这个问题,我将行改为table=汤。全部找到('table')[1]然后行=表.find\u all('tr')

相关问题 更多 >