好吧,你确实可以通过以下方式加快速度:
- 深入到底层 - 查看正在发出哪些基本请求以及simulate http://docs.python-requests.org/en/latest/ them
-
let BeautifulSoup use lxml parser http://www.crummy.com/software/BeautifulSoup/bs4/doc/#specifying-the-parser-to-use
- use SoupStrainer http://www.crummy.com/software/BeautifulSoup/bs4/doc/#parsing-only-part-of-a-document仅解析页面的相关部分
既然这是ASP.NET
生成的表单,由于它的安全功能,事情变得有点复杂。这是完整的代码,不要害怕 - 我已经添加了评论并欢迎提问:
import re
from bs4 import BeautifulSoup, SoupStrainer
import requests
# start session and get the search page
session = requests.Session()
response = session.get('https://acadinfo.wustl.edu/Courselistings/Semester/Search.aspx')
# parse the search page using SoupStrainer and lxml
strainer = SoupStrainer('form', attrs={'id': 'form1'})
soup = BeautifulSoup(response.content, 'lxml', parse_only=strainer)
# get the view state, event target and validation values
viewstate = soup.find('input', id='__VIEWSTATE').get('value')
eventvalidation = soup.find('input', id='__EVENTVALIDATION').get('value')
search_button = soup.find('input', value='Search')
event_target = re.search(r"__doPostBack\('(.*?)'", search_button.get('onclick')).group(1)
# configure post request parameters
data = {
'__EVENTTARGET': event_target,
'__EVENTARGUMENT': '',
'__LASTFOCUS': '',
'__VIEWSTATE': viewstate,
'__EVENTVALIDATION': eventvalidation,
'ctl00$Body$ddlSemester': '201405',
'ctl00$Body$ddlSession': '',
'ctl00$Body$ddlDept': '%',
'ctl00$Body$ddlAttributes': '0',
'ctl00$Body$Days': 'rbAnyDay',
'ctl00$Body$Time': 'rbAnyTime',
'ctl00$Body$cbMorning': 'on',
'ctl00$Body$cbAfternoon': 'on',
'ctl00$Body$cbEvening': 'on',
'ctl00$Body$tbStart': '9:00am',
'ctl00$Body$tbEnds': '5:00pm',
'ctl00$Body$ddlUnits': '0',
'ctl00$Body$cbHideIStudy': 'on',
'ctl00$Body$courseList$hidHoverShow': 'Y',
'ctl00$Body$courseList$hidDeptBarCnt': '',
'ctl00$Body$courseList$hidSiteURL': 'https://acadinfo.wustl.edu/Courselistings',
'ctl00$Body$courseList$hidExpandDetail': '',
'ctl00$Body$hidDay': ',1,2,3,4,5,6,7',
'ctl00$Body$hidLevel': '1234',
'ctl00$Body$hidDefLevel': ''
}
# get the list of options
strainer = SoupStrainer('div', attrs={'id': 'Body_courseList_tabSelect'})
options = soup.select('#Body_ddlSchool > option')
for option in options:
print "Processing {option} ...".format(option=option.text)
data['ctl00$Body$ddlSchool'] = option.get('value')
# make the search post request for a particular option
response = session.post('https://acadinfo.wustl.edu/Courselistings/Semester/Search.aspx',
data=data)
result_soup = BeautifulSoup(response.content, parse_only=strainer)
print [item.text[:20].replace(' ', ' ') + '...' for item in result_soup.select('div.CrsOpen')]
Prints:
Processing Architecture ...
[u'A46 ARCH 100...', u'A46 ARCH 111...', u'A46 ARCH 209...', u'A46 ARCH 211...', u'A46 ARCH 266...', u'A46 ARCH 305...', u'A46 ARCH 311...', u'A46 ARCH 323...', u'A46 ARCH 328...', u'A46 ARCH 336...', u'A46 ARCH 343...', u'A46 ARCH 350...', u'A46 ARCH 355...', u'A46 ARCH 411...', u'A46 ARCH 422...', u'A46 ARCH 428...', u'A46 ARCH 436...', u'A46 ARCH 445...', u'A46 ARCH 447...', u'A46 ARCH 465...', u'A48 LAND 451...', u'A48 LAND 453...', u'A48 LAND 461...']
Processing Art ...
[u'F10 ART 1052...', u'F10 ART 1073...', u'F10 ART 213A...', u'F10 ART 215A...', u'F10 ART 217B...', u'F10 ART 221A...', u'F10 ART 231I...', u'F10 ART 241D...', u'F10 ART 283T...', u'F10 ART 301A...', u'F10 ART 311E...', u'F10 ART 313D...', u'F10 ART 315B...', u'F10 ART 317H...', u'F10 ART 323A...', u'F10 ART 323B...', u'F10 ART 323C...', u'F10 ART 329C...', u'F10 ART 337E...', u'F10 ART 337F...', u'F10 ART 337H...', u'F10 ART 385A...', u'F10 ART 391M...', u'F10 ART 401A...', u'F10 ART 411E...', u'F10 ART 413D...', u'F10 ART 415B...', u'F10 ART 417H...', u'F10 ART 423A...', u'F10 ART 423B...', u'F10 ART 423C...', u'F10 ART 429C...', u'F10 ART 433C...', u'F10 ART 433D...', u'F10 ART 433E...', u'F10 ART 433K...', u'F10 ART 461C...', u'F10 ART 485A...', u'F20 ART 111P...', u'F20 ART 115P...', u'F20 ART 1186...', u'F20 ART 119C...', u'F20 ART 127A...', u'F20 ART 133B...', u'F20 ART 135G...', u'F20 ART 135I...', u'F20 ART 135J...', u'F20 ART 1361...', u'F20 ART 1363...', u'F20 ART 1713...', u'F20 ART 219C...', u'F20 ART 2363...', u'F20 ART 2661...', u'F20 ART 281S...', u'F20 ART 311P...', u'F20 ART 315P...', u'F20 ART 3183...', u'F20 ART 333B...', u'F20 ART 335A...', u'F20 ART 335J...', u'F20 ART 3713...', u'F20 ART 381S...', u'F20 ART 415P...', u'F20 ART 435I...']
...
这里肯定有一些需要改进的地方,例如,我已经对其他表单值进行了硬编码 - 您可能应该解析可能的值并适当地设置它们。
另一个改进是将其与grequests https://github.com/kennethreitz/grequests:
GRequests 允许您将 Requests 与 Gevent 一起使用来进行异步
轻松进行 HTTP 请求。
正如您所看到的,当您处于较高级别并通过网络驱动程序与浏览器交互时,您并不担心发送到服务器以获取数据的实际请求。这使得自动化变得很容易,但速度可能会非常慢。当您进入低级自动化时,您有更多选择来加快速度,但实现复杂性增长得非常快。另外,请考虑一下这种解决方案的可靠性如何。所以可能会坚持“黑匣子”解决方案并继续使用selenium
?
我还尝试使用以下方法解决问题:
- mechanize https://pypi.python.org/pypi/mechanize/
- robobrowser https://github.com/jmcarp/robobrowser
- mechanicalsoup https://github.com/hickford/MechanicalSoup
但由于不同原因失败(可以为您提供相关错误信息)。不过,所有这 3 个工具都应该有助于简化解决方案。
另请参阅类似主题:
- 使用 python 将请求发送到 asp.net 页面 https://stackoverflow.com/questions/14746750/post-request-using-python-to-asp-net-page
- 如何在python中向.aspx页面提交查询 https://stackoverflow.com/questions/1480356/how-to-submit-query-to-aspx-page-in-python
- 向 aspx 页面提交 post 请求 https://stackoverflow.com/questions/6269064/submitting-a-post-request-to-an-aspx-page
- 如何与 ASP 网页相处 https://blog.scraperwiki.com/2011/11/how-to-get-along-with-an-asp-webpage/