Skip to main content

fetch in Python using your browser!

Project description

Fetch using your browser.

Let the browser manage cookies for you.

⚠️ This project is a very simple implementation. Not tested thoroughly. Consider it a proof of concept.

Usage

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

from asyncio import gather, get_event_loop

from browserfetch import fetch, run_server


async def main():
    response1, response2, reponse3 = await gather(
        fetch('https://example.com/path1'),
        fetch('https://example.com/image.png'),
        fetch('https://example.com/path2'),
    )
    # do stuff with retrieved responses


loop = get_event_loop()
loop.create_task(main())
run_server(loop=loop)
  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 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 a 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.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

browserfetch-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: browserfetch-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for browserfetch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 70ef1decf405f8eaec32648ebe269170116701047c59556f28317cb505eab4ec
MD5 78a12bd037acc7ad9c42ee0ea96935ce
BLAKE2b-256 6a7bafddd1182bdde47266ab1292f647f881deafa4f23ba967aaf059d97c6609

See more details on using hashes here.

File details

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

File metadata

  • Download URL: browserfetch-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for browserfetch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4ca0520a5b2bad3858cf66b49c7b5af65ffa88810ad0848ef406fee4f261a04
MD5 0246a0c12d726c9ccc48e0a48413211b
BLAKE2b-256 18582f0353fb194d15d4bc7495d16c7fb245439efe3962524703a02c64c75f1e

See more details on using hashes here.

Supported by

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