【发布时间】:2016-01-07 04:01:37
【问题描述】:
我正在尝试收集有关大学篮球队的大量数据。此链接:https://www.teamrankings.com/ncb/stats/ 拥有大量团队统计数据。
我试图编写一个脚本,从该页面扫描所有所需的链接(所有团队统计数据),找到指定团队的排名(输入),然后返回所有链接中该团队排名的总和。
我很高兴地找到了这个:https://gist.github.com/phillipsm/404780e419c49a5b62a8
...太棒了!
但我一定有什么问题,因为我得到了 0
这是我的代码:
import requests
from bs4 import BeautifulSoup
import time
url_to_scrape = 'https://www.teamrankings.com/ncb/stats/'
r = requests.get(url_to_scrape)
soup = BeautifulSoup(r.text, "html.parser")
stat_links = []
for table_row in soup.select(".expand-section li"):
table_cells = table_row.findAll('li')
if len(table_cells) > 0:
link = table_cells[0].find('a')['href']
stat_links.append(link)
total_rank = 0
for link in stat_links:
r = requests.get(link)
soup = BeaultifulSoup(r.text)
team_rows = soup.select(".tr-table datatable scrollable dataTable no-footer tr")
for row in team_rows:
if row.findAll('td')[1].text.strip() == 'Oklahoma':
rank = row.findAll('td')[0].text.strip()
total_rank = total_rank + rank
print total_rank
查看该链接以仔细检查我是否指定了正确的类。我感觉问题可能出在第一个 for 循环中,我选择了一个 li 标签,然后选择了第一个标签中的所有 li 标签,我不知道。
我不使用 Python,所以我不熟悉任何调试工具。因此,如果有人想将我转发给那些会很棒的人之一!
【问题讨论】:
-
可以在python repl中测试每一步
标签: python html beautifulsoup python-requests