Python 通过带反爬虫保护的表单发送请求
我正在用Python从一个网站上抓取内容,这个网站有一个简单的表单认证,需要输入用户名和密码。不过,它还有一个叫“foil”的隐藏字段,每次加载页面时,这个字段里会有一个看起来像随机生成的字符串。为了成功登录,这个值必须包含在发送请求的内容头里。我尝试在登录页面加载后抓取这个随机字符串,但还是被重定向回登录页面。我有一个有效的用户名和密码可以用,但这个信息会不定期更新,我想在有变化时给自己发个邮件。以下是我到目前为止写的代码...
import urllib, urllib2, cookielib,subprocess
url='https://example.com/login.asp'
username='blah'
password='blah'
request = urllib2.Request(url)
opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1))
preData = opener.open(request).readlines()
for line in preData:
if("foil" in line):
foils = line.split('"')
notFoiled = foils[3]
query_args={'location':'','qstring':'','absr_ID':notFoiled,'id':username,'pin':password,'submit':'Sign In'}
requestWheader = urllib2.Request('https://example.com/login.asp')
requestWheader.add_data(urllib.urlencode(query_args))
print 'Request method after data :', requestWheader.get_method()
print
print 'OUTGOING DATA:'
print requestWheader.get_data()
print
print 'SERVER RESPONSE:'
print urllib2.urlopen(requestWheader).read()
rawRes = urllib2.urlopen(requestWheader).read()
这个表单看起来是这样的...
<form name="loginform" method="post" action="https://example.com/login.asp?x=x&&pswd=">
<input type=hidden name="location" value="">
<input type=hidden name="qstring" value="">
<input type=hidden name="absr_ID" value="">
<input type=hidden name="foil" value="91fcMO">
<input type="text" name="id" maxlength="80" size="21" value="" mask="" desc="ID" required="true">
<input type="submit" name="submit" value="Sign In" onClick="return checkForm(loginform)">
<input type="password" name="pin" size="6" maxlength="6" desc="Pin" required="true">
1 个回答
2
你引入了 cookielib
,但看起来你并没有使用任何 CookieJar
:
jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
然后在获取初始表单和提交登录表单时,使用同一个打开器。我猜这是基于cookie的保护机制,其中来自 foil
字段的值必须和头部中的cookie匹配。
我还注意到你在代码中把 notFoiled
赋值给了 absr_ID
,而不是 foil
。这是故意这样做的吗?
另外,建议你使用 html5lib
或 BeautifulSoup
,而不是手动解析HTML,这样会更方便。