原博主用的是PyV8执行360问答JS代码,我换了PyExecJ阻天关把S
importexecjs
密满路地兴importre
走药非脱宁旧维编importrequests
url=""
HERDERS={
"Host":"www.kuaidaili.com",
'User-Agent':'Mozilla/5.0(WindowsN南伤江诗半副T6.1;WOW64)Ap利左存势民阳市对说行居pleWebKit/537.36(KHTML,like抗照军Gecko)Chrome/49.0.2623.87Safari/537.36',
}
defexecutejs(html):
#提取其中的JS加密函数
js_string=''.join(re.findall(r'(function.*?)</script>',html)且几片清)
#提取其中执行JS函数的参数
js_func_arg=re.fin宜段边调dall(r'setTimeout\(\"\D+\((\d+)\)\"',万品艺供混策讲本html)[0]
js_func_name=re.findall(r'function(\w+)',js_string)[0]
#修改JS函数,使其返回Cookie内容
js_stri挥威调ng=js_st效阳ring.replace('eval("qo=eval;qo(po);")','returnpo')
func=execjs.compile(js_string)
returnfunc.call(js_func_nam根假e,js_func_arg)
defparse_cookie(string):
string=string.replace("document.cookie='","")
clearance=s永液轻tring.split(';')[0]
retu者后修业探王价rn{clearance.split('=')[0]:clearance.split('=')[1]}
#第一列调次访问获取动态加密取余买散龙印井投的JS
first_ht她要年玉以ml=requests.get(url=url,headers=HERDERS).content.decode('utf-8')
#执行JS获取Cookie
cookie_str=executejs(first_html)
#将Cookie转换为字典格式
cookie=parse_cookie(cookie_str)
print('cookies=',cookie)
#带上cookies参数,再次请求
response=requests.get(url=url,headers=HERDERS,cookies=cookie)
print(response.status_code)
标签:python,http,error