用了网上的python非API方式模拟登录新浪微薄的代码,登录成功后抓取微薄个人页面信息。同样的一个微薄页面,用urllib2的方式抓取一点问题都没,如果用tornado.httpclient.HTTPClient().fetch(url)的方式抓取就会跳转到登录界面,而且始终登录不成功,何解?注:如果是urllib2方式的话所有页面都抓取成功;tornadohttpclient的方式只是部分页面会跳转到登录地址,还是有些页面可以成功抓取的。微薄登录代码:https://github.com/yoyzhou/weibo_login/edit/master/weibo_login.py抓取微薄代码如下:#!/usr/bin/envpython#encoding:utf-8importurllib2importgzipimporttornado.genimporttornado.httpclientfromweibo_loginimportlogindefurllib2_type(url):headers={'User-Agent':'Mozilla/5.0(X11;Linuxx86_64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/34.0.1847.116Safari/537.36'}request=urllib2.Request(url=url,headers=headers)response=urllib2.urlopen(request)print'urllib2get:%s'%response.urldeftornado_type(url):http_header={'User-Agent':'Mozilla/5.0(X11;Linuxx86_64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/34.0.1847.116Safari/537.36'}http_request=tornado.httpclient.HTTPRequest(url=url,method='GET',headers=http_header,\use_gzip=True,connect_timeout=200,request_timeout=600)http_client=tornado.httpclient.HTTPClient()http_response=http_client.fetch(http_request)print'tornadohttpclientget:%s'%http_response.effective_urlif__name__=='__main__':username=''pwd=''cookie_file='weibo_login_cookies.dat'url='http://weibo.com/zhaodong1982'iflogin(username,pwd,cookie_file):print'LoginWEIBOsucceeded'urllib2_type(url)tornado_type(url)运行结果如下,httpclient的方式地址被重定向到注册界面了:(venv)➜testgit:(mongoengine)✗pythontest.pyLoadingcookiessuccessLoginWEIBOsucceededurllib2get:http://weibo.com/zhaodong1982tornadohttpclientget:http://weibo.com/signup/signup.php?inviteCode=1658066713
添加回答
举报
0/150
提交
取消