本文實例講述了Python實現模擬登錄網易郵箱的方法。分享給大家供大家參考,具體如下:
#coding:utf-8import urllib2,urllibimport cookielibfrom bs4 import BeautifulSoup#設置代理IPproxy_support = urllib2.ProxyHandler({'http':'120.197.234.164:80'})#設置cookiecookie_support = urllib2.HTTPCookieProcessor(cookielib.LWPCookieJar())opener = urllib2.build_opener(proxy_support,cookie_support,urllib2.HTTPHandler)urllib2.install_opener(opener)#開始的URL#hosturl = "http://www.renren.com"hosturl = "http://mail.163.com/"#接受表單數據的URL#posturl = "http://www.renren.com/ajaxLogin/login"posturl = "https://mail.163.com/entry/cgi/ntesdoor?df=mail163_letter&from=web&funcid=loginone&iframe=1&language=-1&passtype=1&product=mail163&net=e&style=-1&race=118_35_39_bj&uid=Thinkgamer@163.com"#發送表單數據postdata = urllib.urlencode( { "username":"xxxxxxxxxxx", "password":"xxxxxxxxxxxxxxx" })#設置表頭headers = { #'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0/', #'Referer':'http://www.renren.com/' 'User-Agent':"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0", 'Referer':'http://mail.163.com/'}#生成HTTP請求req =urllib2.Request( url = posturl, data = postdata, headers = headers)print reqpage = urllib2.urlopen(req).read()print pagelistvalue = page.split(";")url = listvalue[0].split("op.location.href = ")[1]href = url[1:-1]print hrefsoup = BeautifulSoup(urllib2.urlopen(href))print soup.title
推薦一篇不錯的文章:https://www.jb51.net/article/90114.htm
更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答