内网有个平台是ASP.net,刚开始没想到这么复杂。。开弓没有回头箭。。
每打开一个网页都会有三次请求如图:
<span style="font-family: Arial, Helvetica, sans-serif;">且每次请求所包含的 __VIEWSTATE还有其他一些数据均不同。。。查了一下 __VIEWSTATE,其本身也有防爬的作用,估计也就是增加了爬取的复杂度。</span>
总之这爬虫写的相当累。。
代码中 class 只为了结构上好看些。。。
爬取中正则写疯掉了。。中间还遇到所谓的正则回溯。。卡那没反应也不报错。。
</pre><pre name="code" class="python">
</pre><pre name="code" class="python">#coding=utf-8
import requests
import re
import urllib
import xlwt
from requests_ntlm import HttpNtlmAuth
fw=str(input('FromWeek:'))
tw=str(input('ToWeek:'))
class http:
url='http://xxxxxxxxx'#由于爬的是内网所以匿了
url2='http://xxxxxxxxx='
class head:
'''定义请求头\
因为打开一个网页需要发送三次请求,并且通过抓包发现每次的头都不同\
所以建立三个头'''
h1={
'Accept':'image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*',
'Accept-Language':'zh-cn',
'Host':'timesheet',
'Connection':'Keep-Alive',
'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; InfoPath.3; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)'}
h2=h1.copy()
h2['Content-Type']='application/x-www-form-urlencoded; charset=utf-8'
h3=h2.copy()
h3['Accept']='*/*'
h3['X-MicrosoftAjax']='Delta=true'
h3['Cache-Control']='no-cache'
h3['Referer']='xxxxxxxx'
h3['Pragma']='no-cache'
class get:
'''定义一些正则函数,用来获取所需的信息'''
def __init__(self,text):
self.text=text
def get_vie(self):
rex=r'''<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)"'''
vie=re.findall(rex,self.text)
return vie
def get_eve(self):
rex=r'''<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)"'''
eve=re.findall(rex,self.text)
return eve
def get_ment(self):
rex=r'''<label for="ctl00_ContentMain_ReportViewer_ctl04_ctl11_divDropDown_c.*">(.*?)</label>'''
ment=re.findall(rex,self.text)
return ment
def get_team(self):
rex='''<label for="ctl00_ContentMain_ReportViewer_ctl04_ctl11_divDropDown_ct.*?">(.*?)</label></span></td>'''
team=re.findall(rex,self.text)[1:]
n=''
m=', '
for i in team:
q=re.split(' ',i)
c=''
for a in q:
if a =='&':
a='&'
b=' '
c=c+a+b
n=n+c+m
team=n[:-3]
return team
def get_user(self):
rex='''<label for="ctl00_ContentMain_ReportViewer_ctl04_ctl13_divDropDown_