form表单登陆人人网

import json
from urllib import request,parse
from http import cookiejar
# 创建cookie对象
cookie = cookiejar.CookieJar()
# cookie操作
handler = request.HTTPCookieProcessor(cookie)
# 实例化cookie
opener = request.build_opener(handler)

# 创建url
url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018721954774'

# 提交的form表单
form = {
    'email':'13264423415',
    'origURL':'http://www.renren.com/home',
    'domain':'renren.com',
    'key_id':'1',
    'captcha_type':'web_login',
    'password':'1920a044dee198376b18ac26c02cee55d40cb617ff982cbf3dcf37d8c0d47bbd',
    'rkey':'a2221a76a8bae4493d337a0f2d4fdddd',
    'f':'https%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8%26f%3D3%26rsv_bp%3D1%26ch%3D%26tn%3D95409297_hao_pg%26bar%3D%26wd%3D%25E5%25A4%2596%25E5%259B%25BD%25E4%25BA%25BA%2B%25E4%25B8%25AD%25E6%2596%2587%2B%25E5%25B9%25B3%25E8%25A1%25A1%26oq%3D%2525E5%2525A4%252596%2525E5%25259B%2525BD%2525E4%2525BA%2525BA%2525E4%2525B8%2525AD%2525E6%252596%252587%2525E8%252580%252583%252520%2525E5%2525B9%2525B3%2525E8%2525A1%2525A1%26rsv_pq%3D99eb89d40006d451%26rsv_t%3D7dd3GDJT46Ppgno5EJfXoJJRxAOti4npYCRIeLZCrIrSSwZAUBGpTNSpdnBEfd2cSbJyL3Qn%26rqlang%3Dcn%26rsv_enter%3D1%26prefixsug%3D%2525E5%2525A4%252596%2525E5%25259B%2525BD%2525E4%2525BA%2525BA%252520%2525E4%2525B8%2525AD%2525E6%252596%252587%252520%2525E5%2525B9%2525B3%2525E8%2525A1%2525A1%26rsp%3D2%26inputT%3D4909',
}
# 格式化form表单 设置为bytes类型
form_bytes = parse.urlencode(form).encode('utf-8')
# 发送请求
response = opener.open(url, form_bytes)
# 格式化返回的数据 为bytes类型
html_bytes = response.read()
# 转化为字典类型

res_dict = json.loads(html_bytes.decode('utf-8'))
print(res_dict)
# 获取字典用有用的网址
home_url = res_dict['homeUrl']


# 访问页面
response = opener.open(home_url)
# 转换格式
html = response.read().decode('utf-8')
print(html)






你可能感兴趣的:(爬虫)