简体   繁体   中英

Empty RequestsCookieJar in Python requests

I'm working with Python requests module.

>>> import requests
>>> s1 = requests.Session()
>>> r1 = s1.get("https://www.facebook.com/")
>>> r1.cookies
<RequestsCookieJar[Cookie(version=0, name='fr', ... ]>

ie the RequestsCookieJar is full, and It is full for every url I try. But for Instagram I get:

>>> s2 = requests.Session()
>>> r2 = s2.get("https://www.instagram.com/")
>>> r2.cookies
<RequestsCookieJar[]>

ie the RequestsCookieJar is empty, but it should return something.

Could someone please explain me why this is happening?

Thank you.

Its because of instagram. When you open instagram firstly it won't send you cookies, you have to log in, or make a get request again.

Update: As you want to login, and want csrf token, there is the method:

import json
import requests
import lxml.html

def get_csrf_token(content):
    xpath_data = lxml.html.fromstring(content).xpath('/html/body/script[1]/text()')[0]
    raw_json = xpath_data[xpath_data.find('{'):-1]
    return json.loads(raw_json)["config"]["csrf_token"]

def get_main_page():
    session = requests.Session()
    content = session.get('https://instagram.com')

    csrf_token = get_csrf_token(content.content)
    header = {'x-csrftoken'      : csrf_token,
              'x-requested-with' : 'XMLHttpRequest',
              'User-Agent'       : "Your user agent there",
              "referer"          : 'https://instagram.com',
              "cookie"           : "ig_cb=1",
              "origin"           : 'https://instagram.com'}

    session.headers.update(header)

I assume that you could write the "POST" method to pass through login.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM