Skip to main content

fetch in Python using your browser!

Project description

Fetch using your browser.

Let the browser manage cookies for you.

⚠️ Incomplete. Not tested thoroughly. Consider using Playwright, especially for more complex scenarios.

Usage

  1. You’ll run a Python script containing some code like this:

from asyncio import gather, new_event_loop

from browserfetch import fetch, get, post, run_server


async def main():
    response1, response2, reponse3 = await gather(
        get('https://example.com/path1', params={'a': 1}),
        fetch('https://example.com/image.png'),
        post('https://example.com/path2', data={'a': 1}),
    )
    # do stuff with retrieved responses


loop = new_event_loop()
loop.create_task(start_server())
loop.run_until_complete(main())
  1. Open your browser, goto http://example.com (perhaps solve a captcha and log in).

  2. Copy the contents of browserfetch.js file and paste it in browser’s console. (You can use a browser extensions like violentmonkey/tampermonkey to do this step for you.)

That’s it! Your Python script starts handling requests. The browser tab should remain open of-coarse.

The server can handle multiple websocket connections from different websites simultaneously.

How it works

browserfetch communicates with your browser using a websocket. The fetch function just passes the request to browser and it is the browser that handles the actual request. Response data is sent back to Python using the same WebSocket connection.

Motivations

  • browser_cookie3 stopped working on Chrome-based browsers. There is a workaround: ShadowCopy, but it requires admin privilege.

  • Another issue with browser_cookie’s approach is that it retrieves cookies from cookie files, but these files are not updated instantly. Thus, you might have to wait or retry a few times before you can successfully access newly set cookies.

  • ShadowCopying and File access are slow and inefficient operations.

Downsides

  • Setting up browserfetch is more cumbersome since it requires running a Python server and also injecting a small script into the webpage. Using browser_cookie3 might be a better choice if there are many websites that you need to communicate with.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

browserfetch-0.8.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

browserfetch-0.8.0-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file browserfetch-0.8.0.tar.gz.

File metadata

  • Download URL: browserfetch-0.8.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for browserfetch-0.8.0.tar.gz
Algorithm Hash digest
SHA256 9ed1e65f8cd5a4ec55af9fb0e27ec6a2f019f8a12ad1c212e4e15476020fb650
MD5 3f446d4221ee0daf2e240073954d8ba9
BLAKE2b-256 e7fa4d977f09bbf80e5785472378f45f410388fc00ca62b73f497a5108a8174e

See more details on using hashes here.

File details

Details for the file browserfetch-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: browserfetch-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for browserfetch-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2af4f35aaf66e0e28442819e5e1f6b86bf6529f3a997a5484f6793c13e82b8c9
MD5 9f9a39124cbe8b856713abd4bf5b9d74
BLAKE2b-256 b8d608f281853a61f6ac6dbfa5aa47b3a47ccfbfda270537a1111e3b59e44b64

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page