DEV Community

loading...

How to scrape Reddit using python scrapy

proxiesapi profile image Mohan Ganesan Originally published at proxiesapi.com Updated on ・3 min read

Scrapy is one of the most accessible tools that you can use to scrape and also spider a website with effortless ease.

Today let's see how we can scrape Reddit to get new posts from a subreddit like r/programming.

First, we need to install scrapy if you haven't already.
Once installed, go ahead and create a project by invoking the start project command.
And create a folder structure
Now CD into the scraping project. You will need to do it twice
Now we need a spider to crawl through the programming subreddit. So we use the genspider to tell scrapy to create one for us. We call the spider our first bot and pass it the url of the subreddit
this should return successfully
Great. Now open the file ourfirstbot.py in the spider's folder...
Let's examine this code before we proceed...

The allowed_domains array restricts all further crawling to the domain paths specified here.

start_urls is the list of URLs to crawl... for us, in this example, we only need one URL.

The def parse(self, response): function is called by scrapy after every successful URL crawl. Here is where we can write our code to extract the data we want.

We now need to find the CSS selector of the elements we need to extract the data. Go to the URL https://www.reddit.com/r/programming/ and right-click on the title of one of the posts and click on inspect. This will open the Google Chrome Inspector like below...
You can see that the CSS class name of the title is _eYtD2XCVieq6emjKBH3m so we are going to ask scrapy to get us the text property of this class
Similarly, we try and find the class names of the votes element and the number of comments element (note that the class names might change by the time you run this code.
If you are unfamiliar with CSS selectors, you can refer to this page by Scrapy https://docs.scrapy.org/en/latest/topics/selectors.html

We have to now use the zip function to map a similar index of multiple containers so that they can be used just using a single entity.
And now let's run this with the command.
And Bingo... you get the results
Now, let's export the extracted data to a CSV file. All you have to do is to provide the export file
or if you want the data in the JSON format...
Scaling Scrapy

The example above is ok for small scale web crawling projects. But if you try to scrape large quantities of data at high speeds from websites like Reddit you will find that sooner or later your access will be restricted. Reddit can tell you are a bot so one of the things you can do is to run the crawler impersonating a web browser. This is done by passing the user agent string to the Reddit webserver so it doesn't block you.
In more advanced implementations you will need to even rotate this string so Reddit cant tell it's the same browser! Welcome to web scraping.

If we get a little bit more advanced, you will realize that Reddit can simply block your IP ignoring all your other tricks. This is a bummer and this is where most web crawling projects fail.

Investing in a private rotating proxy service like Proxies API can most of the time make the difference between a successful and headache-free web scraping project which gets the job done consistently and one that never really works.

Plus with the 1000 free API calls running an offer, you have almost nothing to lose by using our rotating proxy and comparing notes. It only takes one line of integration to its hardly disruptive.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

With millions of high speed rotating proxies located all over the world,

With our automatic IP rotation

With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)

With our automatic CAPTCHA solving technology,

hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

The whole thing can be accessed by a simple API like below in any programming language.
We have a running offer of 1000 API calls completely free. Register and get your free API Key here.

Once you have an API_KEY from Proxies API, you just have to change your code
We have only changed one line at the start_urls array and that will make sure we will never have to worry about IP rotation, user agent string rotation, or even rate limits ever again.

Discussion

pic
Editor guide