Python Forum

Full Version: Requests Session Overwriting and cookie jar
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings,

I have two questions in regards to the code below, I will make note of them inside the code itself.

session = requests.Session()

# 1. I assign a session to the session variable but below, I re-assign a session.request to the session variable. Does this overwrite the initial session object? # should I define a new variable?
#2. I need to send and receive cookies or cookie jar on each request. See below.



BASE_URL = "https://www.homeadvisor.com/c.Flooring-Carpet.Dallas.TX.-12032.html"

def get_html(session, BASE_URL, PAGE):

    try:
        # How do I receive and send cookies with each request? I've researched it but the first time you enter a site, their aren't any cookies to be sent.

        PARAMS = {"startingIndex": PAGE}
        session = session.get(BASE_URL, headers=HEADERS, params=PARAMS)   # Is it considered good practice to re-assign this to the session variable?
    
       
    except requests.exceptions.RequestException as e:
        print("Failed to connect to host: " + BASE_URL)
        exit(
            "Check your internet connection. Then try to open the URL in a web browser. Program exiting."
        )
        exit("Could not establish connection to host. Terminating program!")

    return session
Thank you