N
N
nzarchii602020-05-11 20:28:34
Python
nzarchii60, 2020-05-11 20:28:34

Send Cookies with urlib.request method?

I'm trying to send a post request with my data, I need to get cookies from the first call to the server. Put the obtained values ​​into the second request containing the already changed header and user agent.

import urllib.request
import re
from lxml import html
import requests
import time
response = urllib.request.urlopen('https://google.com/auth/login/')
token = response.read()
tree = html.fromstring(token)
csrf = tree.xpath('//*[@id="main"]/div/div/form/input/@value')
kyki = response.headers # куки сохранены в переменную.
kyka = response.info()
ksrf = response.getheader('Set-Cookie')
print(response.getcode()) # ответ сервера. 200 good 400 bad 500
"""ПОЛУЧИТЬ куки и сохранить их для данной сессии. """
response.close()
time.sleep(5)
#cookie=response.info()['Set-Cookie']
#print(one)
url = 'https://google.com/auth/login/'
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Origin': 'https://ate.com',
'Referer': 'https://ate.com/auth/login/',
'Upgrade-Insecure-Requests': '1',
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'}    
#response.add_header('Cookie',kyka)
data = '{"Language":"ru"}{"Params":[null, null, 1, null, null]}{"V":1}{"Adult": false}'
res = requests.post(url, data=data, headers=headers)
print(res.text)
jyj = res.headers  
time.sleep(5)
res = requests.post(url, data=data, headers=jyj)
print(res.text)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-05-11
@dimonchik2013

https://stackoverflow.com/questions/25210197/how-t...
or cookiejar
is easier to attach to requests session

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question