第一种:
post提交密码与账号,进行访问
import urllib.request
import urllib.parse
import http.cookiejar
#在代码中保存cookie
#创建一个cookiejar对象
cj = http.cookiejar.CookieJar()
#通过cj创建一个headler
handler = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(handler)
#提交地址
post_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019731422845'
formdata = {
'email': '邮箱',
'icode': '',
'origURL': 'http://www.renren.com/home',
'domain': 'renren.com',
'key_id': '1',
'captcha_type': 'web_login',
'password': '密码',
'rkey': '5f7a1d96ff48551fe2910f08a1a2ddcd',
'f': 'http%3A%2F%2Fwww.renren.com%2F971784533%2Fnewsfeed%2Fphoto',
}
#处理表单数据
formdata = urllib.parse.urlencode(formdata).encode('utf8')
headers = {
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'www.renren.com',
'Origin': 'http://www.renren.com',
'Referer': 'http://www.renren.com/SysHome.do',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
request =urllib.request.Request(url=post_url,headers=headers)
response = opener.open(request,data=formdata)
# print(response.read().decode('utf8'))
#访问登陆后的资料详情页面
pro_url = 'http://www.renren.com/971784533/profile'
request1 = urllib.request.Request(url=pro_url,headers=headers)
response1 = opener.open(request1)
with open('moni1.html','wb')as f:
f.write(response1.read())
第二种:
也是一种万能方法更具cookie登陆
import urllib.request
url = 'http://www.renren.com/971784533/profile'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'Cookie': '自己的cookie',
}
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
with open('renren.html','wb')as f:
f.write(response.read())