我最近学习了一些关于如何在 Python 中使用 BeautifulSoup 的教程,并学习了如何简单地从网页中抓取文本和 URL。我现在正在尝试从以下链接中抓取数据,
http://www.study.cam.ac.uk/undergraduate/apply/statistics/ http://www.study.cam.ac.uk/undergraduate/apply/statistics/
页面底部有一个交互式图形生成器,我想从中抓取所有数据,而不必花费大量时间繁琐地手写所有可能生成的图形中的值。我尝试使用我微不足道的初学者技术,但对我来说,图表数据来自 HTML 中的何处并不明显 - 此外,HTML 似乎是动态的,具体取决于我的鼠标在屏幕上的位置。
问题:是否可以使用这些工具来抓取这些数据?如果可以,如何抓取?
使用浏览器开发者工具,当您点击Show Graph
按钮有一个POST
请求前往http://www.study.cam.ac.uk/undergraduate/apply/statistics/data.php http://www.study.cam.ac.uk/undergraduate/apply/statistics/data.php。结果是JSON
包含构建图表所需的所有数据的对象。
在 Python 中模拟此请求,例如:requests http://docs.python-requests.org/en/latest/ module:
import requests
URL = "http://www.study.cam.ac.uk/undergraduate/apply/statistics/data.php"
HEADERS = {'X-Requested-With': 'XMLHttpRequest'}
data = {
'when': 'year',
'year': 2014,
'applications': 'on',
'offers': 'on',
'acceptances': 'on',
'groupby': 'college',
'for-5-years-what': 'university'
}
response = requests.post(URL, data=data, headers=HEADERS)
print response.json()
不需要BeautifulSoup
这里。至少,从我从你的问题中了解到的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)