爬取媳妇微博数据(python)
来源:互联网 发布:金山网络官网 编辑:程序博客网 时间:2024/05/24 05:39
写代码的也是有女朋友滴。。因为女朋友也是写代码滴。。
言归正转。
1.最近一直有时间就在看爬虫这方面的,我首先是参考了很多前人的思路,当时他们都说要模拟登录,没有说为什么,然后我当时看了媳妇微博的首页,当时我没有登录,浏览器是可以直接看的,然后 心想应该是可以的呀,天真的我,去尝试了一下,先用的是node,
如果打出来 statusCode 会发现是302
主页的地址,后面更详细的参数也都没填
weibo.com/u/+uid
2.当时我就感慨前辈毕竟是前辈,然后根据前辈的总结,找到了模拟登陆的办法用的是 以及微博模拟登录的加密方式,这些都是前辈的代码。
class Userlogin: def userlogin(self, username,password,pagecount): url_prelogin='https://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=ODA3NzIyODU1JTQwcXEuY29t&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.19)&_=1506407183548' url_login='https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.19)' session = requests.Session() resp = session.get(url_prelogin) json_data=re.findall(r'(?<=\().*(?=\))',resp.text)[0] data = json.loads(json_data) print('data',data) servertime=data['servertime'] nonce = data['nonce'] pubkey = data['pubkey'] rsakv = data['rsakv'] print('username',username) print(base64.b64encode(username.encode(encoding="utf-8"))) su = base64.b64encode(username.encode(encoding="utf-8")) rsaPublickey= int(pubkey,16) key = rsa.PublicKey(rsaPublickey,65537) message = str(servertime)+'\t'+str(nonce)+'\n'+str(password) sp = binascii.b2a_hex(rsa.encrypt(message.encode(encoding='utf_8'),key)) postdata = { 'entry': 'weibo', 'gateway': '1', 'from': '', 'savestate': '7', 'userticket': '1', 'ssosimplelogin': '1', 'vsnf': '1', 'vsnval': '', 'su': su, 'service': 'miniblog', 'servertime': servertime, 'nonce': nonce, 'pwencode': 'rsa2', 'sp': sp, 'encoding': 'UTF-8', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META', 'rsakv' : rsakv, } resp = session.post(url_login,data=postdata)
3.前辈的代码我不用多解释,怎么找到是md5 和rsa 加密的 网上前辈讲的很仔细,这段代码最后发送了post请求 .我得到的结果是这样的
<html><head><title>新浪通行证</title><meta content="0; url='https://login.sina.com.cn/crossdomain2.php?action=login&entry=weibo&r=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack%26ssosavestate%3D1538102016%26ticket%3DST-MjI3MjEzMDUwMQ%3D%3D-1506566016-gz-B5BC6F13782ED5CED17A4A8D208944FF-1%26retcode%3D0'" http-equiv="refresh"/><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/></head><body alink="#ff0000" bgcolor="#ffffff" link="#0000cc" text="#000000" vlink="#551a8b"><script language="javascript" type="text/javascript"> location.replace("https://login.sina.com.cn/crossdomain2.php?action=login&entry=weibo&r=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack%26ssosavestate%3D1538102016%26ticket%3DST-MjI3MjEzMDUwMQ%3D%3D-1506566016-gz-B5BC6F13782ED5CED17A4A8D208944FF-1%26retcode%3D0"); </script></body></html>
4.然后这里应该就是正在登录界面,然后就是意味着 页面还会跳转, 然后就提取出来location.replace,再次请求后,发现会发现又一次得到一个需要跳转的页面
<html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/><title>新浪通行证</title><script charset="utf-8" src="https://i.sso.sina.com.cn/js/ssologin.js"></script></head><body>正在登录 ...<script>try{sinaSSOController.setCrossDomainUrlList({"retcode":0,"arrURL":["https:\/\/passport.weibo.com\/wbsso\/login?ticket=ST-MjI3MjEzMDUwMQ%3D%3D-1506566231-gz-6CAAFA60D3C6956F02A2608B543823AC-1&ssosavestate=1538102231","https:\/\/passport.97973.com\/sso\/crossdomain?action=login&savestate=1538102231","https:\/\/passport.weibo.cn\/sso\/crossdomain?action=login&savestate=1"]});} catch(e){ var msg = e.message; var img = new Image(); var type = 1; img.src = 'https://login.sina.com.cn/sso/debuglog?msg=' + msg +'&type=' + type; }try{sinaSSOController.crossDomainAction('login',function(){location.replace('https://login.sina.com.cn/');});} catch(e){ var msg = e.message; var img = new Image(); var type = 2; img.src = 'https://login.sina.com.cn/sso/debuglog?msg=' + msg +'&type=' + type; }</script></body></html>
5.然后我尝试复制出arrURL里面的地址,然后放入已经有登录cookie的浏览器打开,发现 里面可以得到我自己的uid,虽然我知道自己的uid,但是这一步请求还是必须的,如果不请求,我依然不能成功拿到媳妇的微博, 这个请求发出去后,就可以登录了
add_url= re.findall(r'(?<=arrURL\"\:\[\").*?(?=\")',str(soup_1)) add_url=str(add_url[0]).replace(r'\/','/') respo = session.get(add_url) # uid = re.findall('"uniqueid":"(\d+)",',respo.text)[0] url = "http://weibo.com/u/这里是uid?profile_ftype=1&is_ori=1#_0" respo = session.get(url)
拿到东西后,我发现,前辈说的真对,拿到简单,但是处理起来还真是蛋疼。因为得到的页面经过新浪处理,不是单纯的dom,试试就知道,,还有问题,随时联系我。。。。对了,下个问题是如何控制翻页,网上的思路是有插件可以控制网页滚动的。。 我也尝试看了一下,微博的加载更多,点击加载更多发送了几个请求,但是请求的结果没有需要加载的数据,,,看来还是修行不够,得再向前辈请教
阅读全文
0 0
- 爬取媳妇微博数据(python)
- python爬取数据
- python爬取新浪微博话题的相关数据
- Python爬取Carmax数据
- Python 爬取数据接口
- python 爬取12306数据
- python爬取天气数据
- Python爬取网页数据
- python爬取股票数据
- python爬取股票数据
- 爬取新浪微博数据+新浪微博模拟登录+mysql+python
- 用python爬取新浪微博数据 (无需手动获取cookie)
- python初学-爬取网页数据
- python 爬取一页商品数据
- Python爬取豆瓣电影Top250数据
- Python urllib 爬取基金数据
- 【爬虫】python selenium 爬取数据
- python爬取历史天气数据
- cocos2d-x中TextAtlas艺术字create函数各参数的含义
- numpy库中行与列的浅见
- 鼠标悬浮,产生变化
- 重磅!ABB 26亿美元收购GE工业系统业务
- 产品新人的10字生存手册
- 爬取媳妇微博数据(python)
- 内存核心频率、工作频率,等效频率、预读取技术详解
- HTML 超链接和路径
- 第三周:项目
- Oracle开发专题之:分析函数总结
- 欢迎使用CSDN-markdown编辑器
- CS Academy Round #50 D.Min Races
- 蘑菇阵
- 分享基于Entity Framework的Repository模式设计