我正在尝试从以下页面收集信息:http://www.gatesfoundation.org/How-We-Work/Quick-Links/Grants-Database#q/page=2
特别是,我尝试使用BeautifulSoup从表中收集信息。我有以下代码:
pagelink = 'http://www.gatesfoundation.org/How-We-Work/Quick-Links/Grants-Database#q/page=2'
page = urllib2.urlopen(pagelink)
soup = BeautifulSoup(page)
soup.prettify()
print soup
当我这样做时,表的内容(在“tablebody”标签中)不会显示出来。为什么会这样?如何从这个表中提取信息?你知道吗
你可以用^{} 这样得到它:
您将无法按预期使用
BeautifulSoup4
,因为页面是通过JavaScript呈现的。你知道吗您可以使用dryscrape或selenium。在我看来,Dryscrape对用户更友好,但在Windows上并没有得到官方的支持。你知道吗
另外,请查看avis'关于这一点的优秀答案:
https://stackoverflow.com/a/26440563/1429776
您要查找的内容不是来自该URL的。你知道吗
因此,基本上,当您在Chrome等现代web浏览器中手动浏览页面时,您从该页面看到的内容通常不完全来自您请求的URL。整个过程将是:从最初请求的url获取内容->;解析内容->;加载CSS/JavaScript/图像(大多数情况下从不同的url)->;布局页面/根据CSS/JavaScript请求发出额外请求。它可能看起来像是你得到的仅仅是你最初在地址栏中输入的URL,但实际上浏览器做了大量的幕后工作来完全呈现网页。你知道吗
现在回到您正在浏览的页面,表的内容实际上由JavaScript填充,浏览器首先解析JavaScript,然后发出额外请求以获取内容并呈现为一个完整的页面。你知道吗
您可以使用
Fiddler
或Charles
等工具来捕获整个过程并分析所有流量,以找出幕后发生的情况,在这种情况下,请求获取该表的内容:响应是JSON格式的:
使用内置的
json
模块,您可以轻松地提取所需的信息。你知道吗相关问题 更多 >
编程相关推荐