# How to scrape data anonymously using Tor proxies and Python?

> In this tutorial, we will see how to scrape sites anonymously using a Tor proxy, with Python3 and Torpy library.

- **Author**: Sasha Bouloudnine
- **Published**: 2022-12-16 (16 Dec 2022)
- **Reading time**: 4 min
- **Canonical**: https://www.lobstr.io/blog/how-to-scrape-data-anonymously-using-tor-proxies-and-python
- **Also in**: fr: https://www.lobstr.io/fr/blog/comment-scraper-avec-des-tor-proxies-et-python

---

When scraping online, using one's own server or computer, it can happen that one is blocked by the target website, which then blocks the IP address. This is a *hardblock*, which makes further collection difficult. For example, on the site https://www.carrefour.fr/, protected by the bot-mitigation tool provided by https://www.cloudflare.com/.
![how-to-scrape-data-anonymously-using-tor-proxies-and-python-image-1.png](https://d37gzvgyugjozl.cloudfront.net/how_to_scrape_data_anonymously_using_Tor_proxies_with_Torpy_and_Python_image_1_bf9a0a86af.png "width=628;height=331")

It is then particularly tempting to use an external proxy provider, such as the solid https://brightdata.com/, or the very user-friendly https://www.scrapingbee.com/. And so change your IP to hide your real identity and bypass the blocking. However, these providers often offer an expensive service - 0.5 EUR per GB at brightdata for example.

In this tutorial, we'll look at h**ow to use the Tor proxy network, The Onion Router, with Python 3 and [Torpy](https://github.com/torpyorg/torpy) to browse online.** It's a collaborative, decentralized network, where the message you send goes through a series of distinct identities before it reaches its destination, called Onion Routing.

![how-to-scrape-data-anonymously-using-tor-proxies-and-python-image-2.png](https://d37gzvgyugjozl.cloudfront.net/how_to_scrape_data_anonymously_using_Tor_proxies_with_Torpy_and_Python_image_2_488be4bb21.png "width=455;height=259")

Hence this nice logo that takes the shape of an onion.
The complete code is available here. 

Let's go!

## Prerequisites

In order to complete this tutorial from start to finish, be sure to have **the following installed** on your computer.

* [python3 ](https://www.codecademy.com/article/install-python3)
* [SublimeText](https://www.sublimetext.com/3)

You can click on the links below, which will direct you either to an installation tutorial or to the site in question.

To clarify the purpose of each of the above mentioned elements: python3 is the computer language with which we will scrape the pdf, and SublimeText is a text editor. Sublime.

Let's play!

## Setup

We will proceed as follows: 

1. download tor
2. install tor
3. install torpy

For the first step, just go here: 
[https://www.torproject.org/download/](https://www.torproject.org/download/)

Then download the browser that corresponds to your operating system. 

Here for me, Mac OS: 

![how-to-scrape-data-anonymously-using-tor-proxies-and-python-image-5.png](https://d37gzvgyugjozl.cloudfront.net/how_to_scrape_data_anonymously_using_Tor_proxies_with_Torpy_and_Python_image_3_a791fbe9c8.png "width=628;height=329")

And quietly follow the installation instructions:

![how-to-scrape-data-anonymously-using-tor-proxies-and-python-image-3.png](https://d37gzvgyugjozl.cloudfront.net/how_to_scrape_data_anonymously_using_Tor_proxies_with_Torpy_and_Python_image_4_9941b4f864.png "width=454;height=347")

Finally, we will install [the Python Torpy library](https://github.com/torpyorg/torpy), and [requests](https://requests.readthedocs.io/en/latest/) that allows to move on the Internet with Python : 


```bash
$ pip3 install requests
$ pip3 install torpy
```

And now we're ready to scrape.

NB: with **273 stars**, 43 forks, and the most recent commit on **04/15/2021**, the Torpy library is the most popular, easiest to use, and best maintained Tor access library via Python 3 

🌟

## Code

Here is the complete code: 


```python
# We do import TorRequests class from torpy library
from torpy.http.requests import TorRequests

print('start')
with TorRequests() as tor_requests:

	# We do a first request to ipify.org with a Tor proxy
	print("build circuit #1")
	with tor_requests.get_session() as sess:
		print(sess.get("https://api.ipify.org/").text)

	# We do a second request to ipify.org with a Tor proxy
	print("build circuit #2")
	with tor_requests.get_session() as sess:
		print(sess.get("https://api.ipify.org/").text)

print('~~success')
```

The code is broken down into 3 distinct parts: 


1. we import the [torpy library](https://github.com/torpyorg/torpy)
2. instantiate a Tor session
3. we request [https://api.ipify.org/](https://api.ipify.org/) which returns our IP address

And when we run the code from the terminal:

```bash
$ python3 torpy-tor-proxies-python-tutorial.py
start
build circuit #1
185.220.100.252
build circuit #2
185.220.101.33
~~success
```

So we can see that every time a session is opened, a new IP address is assigned to us.

It's a success to make us cry!

✨

## Benefits

This code will allow you, in 50 seconds, to access, from Python 3 and using the Torpy library, the Tor proxy network.

[According to Tor Metrics](https://metrics.torproject.org/relayflags.html), in 2022 the network will have between **1000 and 2000 exit IPs**, which is what you'll find instead of exit. That means you'll be able to rely on a pool of IPs of that size:

![how-to-scrape-data-anonymously-using-tor-proxies-and-python-image-4.png](https://d37gzvgyugjozl.cloudfront.net/how_to_scrape_data_anonymously_using_Tor_proxies_with_Torpy_and_Python_image_5_1389f70a83.png "width=628;height=353")

In other words, you will be able to

* use a pool of 1000-2000 IP addresses
* anonymize your browsing
* for free

Wonderful!

🧅

## Limitations

Beware, if the network of IPs is free, **the size of the network is relatively small**. For comparison, the market leading proxies provider [Brightdata](https://brightdata.com/) promises a network of over 1.5 million datacenter IPs. That's 1,000 times larger, no less.

Moreover, in addition to being small and accessible by everyone, the network is used when browsing the Darknet, and [take part in more or less legal activities](https://www.vice.com/en/article/3daqxb/study-claims-dark-web-sites-are-most-commonly-used-for-crime). Also, **you take the risk of being quickly blocked by a target site**. 

You can however normally access Google, when you are lucky: 

![how-to-scrape-data-anonymously-using-tor-proxies-and-python-image-5.png](https://d37gzvgyugjozl.cloudfront.net/how_to_scrape_data_anonymously_using_Tor_proxies_with_Torpy_and_Python_image_6_0fab0505c7.png "width=628;height=463")

Finally, since we have to go through a network of servers to guarantee the anonymity of the request, the famous Onion Routing, **the request speed is relatively slow**. For example, if we calculate the access request to https://api.ipify.org/, between the Tor network and a classic IP from [Brightdata](https://brightdata.com/), we have a **speed difference of 1 to 4**.

The result of the script below: 


```bash
$ python3 test-speed-tor-vs-brightdata.py

tor
ip 185.82.127.25
delay 3.131886832998134

brightdata
ip 185.255.166.252
delay 0.8867947079997975

~~success
```

To advance masked yes, but to advance slowly.

🐌

## Conclusion

And that's the end of the tutorial!

![test image size 20240403](https://d37gzvgyugjozl.cloudfront.net/test_image_size_20240403_1b0bac4300.png "width=84;height=68")

In this tutorial, we've seen **how to use Tor network proxies with Python 3, and Torpy**, the latest and easiest to use library on the market.

If you have any questions, or if you need a custom, robust, scalable scraping service that can use a large and powerful IP pool, [contact us here](https://www.lobstr.io/custom).

Happy scraping!

🦀

---

## Related articles

- [Data scraping is illegal!](https://www.lobstr.io/blog/data-scraping-illegal)
- [How to download ebooks from .onion with Python3 and requests?](https://www.lobstr.io/blog/download-ebooks-from-onion-link-python-requests)
- [How to scrape PDFs with Python3 and Tika library?](https://www.lobstr.io/blog/how-to-scrape-pdfs-with-python3-and-tika-library)

Tags: Tutorials, Code, Python, Web Scraping

_Canonical HTML: https://www.lobstr.io/blog/how-to-scrape-data-anonymously-using-tor-proxies-and-python_

More for agents: https://www.lobstr.io/llms.txt, pricing in Markdown: https://www.lobstr.io/pricing.md
