日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python登录代码思路_终于找到一个思路比较清晰的可以模拟登录百度的代码!

發布時間:2024/9/19 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python登录代码思路_终于找到一个思路比较清晰的可以模拟登录百度的代码! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該樓層疑似違規已被系統折疊?隱藏此樓查看此樓

轉自:http://outofmemory.cn/code-snippet/11595/

# -*- coding: utf8 -*-import urllib2import urllibimport cookielibimport reimport bs4URL_BAIDU_INDEX = u'http://www.baidu.com/';#https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true 也可以用這個URL_BAIDU_TOKEN = 'https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&class=login';URL_BAIDU_LOGIN = 'https://passport.baidu.com/v2/api/?login';#設置用戶名、密碼username = '';password = '';#設置cookie,這里cookiejar可自動管理,無需手動指定cj = cookielib.CookieJar();opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));urllib2.install_opener(opener);reqReturn = urllib2.urlopen(URL_BAIDU_INDEX);#獲取token,tokenReturn = urllib2.urlopen(URL_BAIDU_TOKEN);matchVal = re.search(u'"token" : "(?P.*?)"',tokenReturn.read());tokenVal = matchVal.group('tokenVal');#構造登錄請求參數,該請求數據是通過抓包獲得,對應https://passport.baidu.com/v2/api/?login請求postData = { 'username' : username, 'password' : password, 'u' : 'https://passport.baidu.com/', 'tpl' : 'pp', 'token' : tokenVal, 'staticpage' : 'https://passport.baidu.com/static/passpc-account/html/v3Jump.html', 'isPhone' : 'false', 'charset' : 'UTF-8', 'callback' : 'parent.bd__pcbs__ra48vi' };postData = urllib.urlencode(postData);#發送登錄請求loginRequest = urllib2.Request(URL_BAIDU_LOGIN,postData);loginRequest.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');loginRequest.add_header('Accept-Encoding','gzip,deflate,sdch');loginRequest.add_header('Accept-Language','zh-CN,zh;q=0.8');loginRequest.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36');loginRequest.add_header('Content-Type','application/x-www-form-urlencoded');sendPost = urllib2.urlopen(loginRequest);#查看貼吧個人主頁 ,測試是否登陸成功,由于cookie自動管理,這里處理起來方便很多#http://tieba.baidu.com/home/main?un=XXXX&fr=index 這個是貼吧個人主頁,各項信息都可以在此找到鏈接teibaUrl = 'http://tieba.baidu.com/f/like/mylike?v=1387441831248'content = urllib2.urlopen(teibaUrl).read();content = content.decode('gbk').encode('utf8');print content;#解析數據,用的BeautifulSoup4,感覺沒有jsoup用的爽soup = bs4.BeautifulSoup(content);list = soup.findAll('tr');list = list[1:len(list)];careTeibalist = [];print '貼吧鏈接\\t吧名\\t等級';for elem in list: soup1 = bs4.BeautifulSoup(str(elem)); print 'http://tieba.baidu.com/'+soup1.find('a')['href']+'\\t'+soup1.find('a')['title']+'\\t'+soup1.find('a',{'class','like_badge'})['title'];#該片段來自于http://outofmemory.cn

總結

以上是生活随笔為你收集整理的python登录代码思路_终于找到一个思路比较清晰的可以模拟登录百度的代码!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。