爬虫设置cookie

from day01.tuozhan_all import post,get
import json 
from urllib import request,parse

from http import cookiejar
cookie_object = cookiejar.CookieJar()
handler = request.HTTPCookieProcessor(cookie_object)
opener = request.build_opener(handler)

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

# form
form = {
    'email': '18510556963',
    'icode': '',
    'origURL': 'http://www.renren.com/home',
    'domain': 'renren.com',
    'key_id': '1',
    'captcha_type': 'web_login',
    'password': '95cb2a1d59b918e0d16ab5d3535fb40103e4b546e651a3e3c99b91876927c78a',
    'rkey': 'a7bccfbafd7ee702247450942dff5611',
    'f': 'http%3A%2F%2Fwww.renren.com%2F966927992',
}

form_bytes = parse.urlencode(form).encode('utf-8')

response = opener.open(url,form_bytes)

html_bytes = response.read()

res_dict = json.loads(html_bytes.decode('utf-8'))

home_url = res_dict['homeUrl']

response = opener.open(home_url)
html_bytes = response.read()
print(html_bytes.decode('utf-8'))









 

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