python 自动登录
import urllib.parse,urllib.request,http.cookiejar
cookie = http.cookiejar.CookieJar()
cookieProc = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(cookieProc)
urllib.request.install_opener(opener)
def GetUrlRequest(iUrl,iStrPostData,header):
postdata=urllib.parse.urlencode(iStrPostData)
postdata=postdata.encode(encoding='UTF8')
req= urllib.request.Request(
url = iUrl,
data = postdata,
headers = header)
result=urllib.request.urlopen(req).read().decode("UTF8")
return result
header = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0',
'Referer' : 'https://accounts.douban.com/login' }
iStrPostData = {
'form_email' : '自己的账号',
'form_password' : '密码'
}
iUrl='https://accounts.douban.com/login'
GetUrlRequest(iUrl,iStrPostData,header)
for i in range(0, 3):
url='https://www.douban.com/?p='+str(i)
req=urllib.request.Request(url=url,headers=header)
result=urllib.request.urlopen(req).read().decode("UTF8")
print(url)
print(result)