L
L
Lkomi2021-11-14 04:47:32
Parsing
Lkomi, 2021-11-14 04:47:32

How to import cookies from a file in requests?

I need to import cookies from a file in requests and send the request.

def cookies_from_file(cookie_dir):
              with open(cookie_dir) as file:
                cookies_data = file.read().replace('\n','').split(';')
              cookies_dic = {}
              for x in cookies_data:
                key = x.split('=')[0]
                value = x.split('=')[1]
                cookies_dic[key]=value
              return cookies_dic

            cookies = cookies_from_file(cookie_dir)
            response = requests.get(link4,headers=headers,cookies=cookies, timeout=2).text
            soup = BeautifulSoup(response, 'html.parser')

Cookies are written to a variable, but do not work in the request

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question