python3登录网页(163邮箱)实例
# -*- coding: utf-8 -*-
import urllib
import http.cookiejar as cookielib
import urllib.request as urllib2
#创建cookie
cookie = cookielib.CookieJar()
cookieProc = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(cookieProc)
urllib2.install_opener(opener)
#登录网页
data = {'username':'xxx@163.com','password':'xxxxxx','type':1}
post_data = urllib.urlencode(data).encode('UTF-8')
header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
request = urllib2.Request(url = 'http://reg.163.com/logins.jsp',data = post_data,headers = header)
res = urllib2.urlopen(request).read().decde('UTF-8')
#如果上面代码没有install_opener(),则这里可用:opener.open(req).read()方法来获取内容。
print(res)
posted on 2014-04-27 16:44 SpringStudio 阅读(972) 评论(0) 编辑 收藏 举报