【发布时间】:2015-06-27 12:32:45
【问题描述】:
所以我正在尝试使用 BeautifulSoup 和 urllib 从特定网站上抓取表格。我的目标是从此表中的所有数据创建一个列表。我曾尝试使用其他网站的表格使用相同的代码,并且效果很好。然而,在这个网站上尝试它时,该表返回一个 NoneType 对象。有人可以帮我弄这个吗?我曾尝试在网上寻找其他答案,但运气不佳。
代码如下:
import requests
import urllib
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib.request.urlopen("http://www.teamrankings.com/ncaa-basketball/stat/free-throw-pct").read())
table = soup.find("table", attrs={'class':'sortable'})
data = []
rows = table.findAll("tr")
for tr in rows:
cols = tr.findAll("td")
for td in cols:
text = ''.join(td.find(text=True))
data.append(text)
print(data)
【问题讨论】:
-
您查看过此页面的 html 吗?没有桌子……
-
如果您右键单击表格并点击“检查元素”,它会显示带有表格的 html。如果您右键单击页面上的其他任何位置,它将不会显示它。
-
您必须向 Selinium 发出完整的浏览器请求才能获得通过 AJAX/JS 生成的内容
标签: python beautifulsoup screen-scraping urllib