cURL to Code Converter · Tutorial

How to turn a browser request into a Python script

7 min readbeginnerUpdated

You want to automate something a website does, like fetching search results, but you don’t know which request to make. Browser developer tools can copy any request as curl; you will convert it to Python and tidy it up.

What you need

  • A desktop browser with developer tools (Chrome, Edge, Firefox or Safari)
  • The cURL to Code Converter
  • Python 3 with pip install requests

Steps

  1. Find the request

    Open developer tools (F12), go to the Network tab and do the action on the page. Filter by Fetch/XHR to see API calls, and click one whose response contains the data you want.

  2. Copy it as cURL

    Right-click the request and choose Copy › Copy as cURL (on Windows, choose the bash version). Paste it into the converter. The example shows a typical result.

  3. Read the Python code

    The query string stays in the URL, headers become a dictionary and the cookie header becomes a separate cookies dictionary. Run the script to check it returns the same data.

    cookies = {
        "session": "abc123",
        "currency": "EUR",
    }
    response = requests.get(url, headers=headers, cookies=cookies)
  4. Remove what isn’t needed

    Delete headers one at a time and rerun. Usually only a few matter, such as accept and the session cookie. A shorter request is easier to maintain.

  5. Move secrets out of the code

    Replace the session value with os.environ["SHOP_SESSION"], so the script can be shared without sharing your login.

Try it yourself

Opens the cURL to Code Converter with this tutorial's sample loaded.

Try it now

Common mistakes

Copying the cmd or PowerShell version
On Windows, browsers also offer “Copy as cURL (cmd)” or PowerShell. The converter accepts them, but the bash version is the most reliable.
Using a session that expires
Cookies copied from the browser stop working when you log out or the session times out. For long-running scripts, use the site’s official API with a token if it has one.
Ignoring the site’s terms
Automating a website can break its terms of use and put load on it. Check whether there is an API, and keep request rates low.

Next steps

Go back to the cURL to Code Converter, or keep going with one of these:

FAQ

Why does the converted request return a login page?

The session cookie or an authorization header is missing or expired. Copy the request again right after loading the page.