Python实现模拟登录网易邮箱的方法示例

yipeiwu_com7年前Python基础

本文实例讲述了Python实现模拟登录网易邮箱的方法。分享给大家供大家参考,具体如下:

#coding:utf-8
import urllib2,urllib
import cookielib
from bs4 import BeautifulSoup
#设置代理IP
proxy_support = urllib2.ProxyHandler({'http':'120.197.234.164:80'})
#设置cookie
cookie_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 req
page = urllib2.urlopen(req).read()
print page
listvalue = page.split(";")
url = listvalue[0].split("op.location.href = ")[1]
href = url[1:-1]
print href
soup = BeautifulSoup(urllib2.urlopen(href))
print soup.title

推荐一篇不错的文章:/post/90114.htm

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python实现两款计算器功能示例

本文实例为大家分享了Python实现计算器功能示例代码,供大家参考,具体内容如下 1.简单计算器 #计算一个表达式的时候,首先肯定是先算括号里面的,再算乘除法,后算加减法 imp...

python实现嵌套列表平铺的两种方法

方法一:使用列表推导式 >>> vec = [[1,2,3],[4,5,6],[7,8,9]] >>> get = [num for elem i...

Python3.x中自定义比较函数

在Python3.x的世界里,cmp函数没有了。那么sorted,min,max等需要比较函数作为参数的函数该如何用呢? 以min函数的定义为例,有两种重载形式: 单参数(一个迭代器):...

python3监控CentOS磁盘空间脚本

Python脚本监控CentOS磁盘空间,任何一个分区空间使用大于80%即发邮件给到指定邮箱。 monitor.py #-*- coding: utf-8 -*- import s...

Python 面试中 8 个必考问题

1、下面这段代码的输出结果是什么?请解释。 def extendList(val, list=[]): list.append(val) return list list...