为了账号安全,请及时绑定邮箱和手机立即绑定

tornado.httpclient.HTTPClient() 抓取 新浪微薄数据

tornado.httpclient.HTTPClient() 抓取 新浪微薄数据

慕妹3242003 2019-03-29 22:08:36
用了网上的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
查看完整描述

2 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

tornado.httpclient.HTTPClient的user-agent是空串,不知道是否因为这个原因被屏蔽了
                            
查看完整回答
反对 回复 2019-03-29
  • 2 回答
  • 0 关注
  • 470 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号