Skip to content

requestsvsmechanize

Apache-2.0 230 30 53,883
1.3 billion (month) Feb 14 2011 2.33.1(2026-03-30 16:09:13 ago)
4,440 8 6 MIT
Jul 25 2009 213.1 thousand (month) 2.14.0(2025-01-05 18:30:46 ago)

The requests package is a popular library for making HTTP requests in Python. It provides a simple, easy-to-use API for sending HTTP/1.1 requests, and it abstracts away many of the low-level details of working with HTTP. One of the key features of requests is its simple API. You can send a GET request with a single line of code:
python import requests response = requests.get('https://webscraping.fyi/lib/requests/') requests makes it easy to send data along with your requests, including JSON data and files. It also automatically handles redirects and cookies, and it can handle both basic and digest authentication. Additionally, it's also providing powerful functionality for handling exceptions, managing timeouts and session, also handling a wide range of well-known content-encoding types. One thing to keep in mind is that requests is a synchronous library, which means that your program will block (stop execution) while waiting for a response. In some situations, this may not be desirable, and you may want to use an asynchronous library like httpx or aiohttp. You can install requests package via pip package manager: shell pip install requests requests is a very popular library and has a large and active community, which means that there are many third-party libraries that build on top of it, and it has a wide range of usage.

Mechanize is a Ruby library for automating interaction with websites. It automatically stores and sends cookies, follows redirects, and can submit forms — making it behave like a web browser without needing an actual browser engine.

Key features include:

  • Automatic cookie management Stores cookies received from servers and sends them back on subsequent requests, maintaining session state across multiple pages.
  • Form handling Can find, fill in, and submit HTML forms programmatically. Supports text inputs, selects, checkboxes, radio buttons, and file uploads.
  • Link following Navigate through pages by clicking links using their text content, CSS selectors, or href patterns.
  • History and back/forward Maintains a browsing history, allowing you to go back and forward through visited pages.
  • HTTP authentication Supports basic and digest HTTP authentication.
  • Proxy support Can route requests through HTTP proxies.
  • Redirect handling Automatically follows HTTP redirects (configurable).

Mechanize is one of the oldest and most established web interaction libraries in Ruby. It is best suited for scraping traditional server-rendered websites with forms and multi-page workflows. For JavaScript-heavy sites, a browser automation tool like Selenium or Playwright is recommended instead.

Highlights


syncease-of-useno-http2no-asyncpopular
popularproduction

Example Use


```python import requests # get request: response = requests.get("http://webscraping.fyi/") response.status_code 200 response.text "text" response.content b"bytes" # requests can automatically convert json responses to Python dictionaries: response = requests.get("http://httpbin.org/json") print(response.json()) {'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why WonderWidgets are great', 'Who buys WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}} # for POST request it can ingest Python's dictionaries as JSON: response = requests.post("http://httpbin.org/post", json={"query": "hello world"}) # or form data: response = requests.post("http://httpbin.org/post", data={"query": "hello world"}) # Session object can be used to automatically keep track of cookies and set defaults: from requests import Session s = Session() s.headers = {"User-Agent": "webscraping.fyi"} s.get('http://httpbin.org/cookies/set/foo/bar') print(s.cookies['foo']) 'bar' print(s.get('http://httpbin.org/cookies').json()) {'cookies': {'foo': 'bar'}} ```
```ruby require 'mechanize' agent = Mechanize.new # Navigate to a page page = agent.get('https://example.com') puts page.title # Find and click a link page = page.link_with(text: 'Products').click # Extract data from the page page.search('.product').each do |product| name = product.at('.name').text price = product.at('.price').text puts "#{name}: #{price}" end # Fill in and submit a login form login_page = agent.get('https://example.com/login') form = login_page.form_with(action: '/login') form['username'] = 'user@example.com' form['password'] = 'password123' dashboard = agent.submit(form) # Cookies are maintained automatically puts dashboard.title # "Dashboard" # Download a file agent.get('https://example.com/report.csv').save('report.csv') ```

Alternatives / Similar


Was this page helpful?