Step-by-step tutorials for you to get started with web scrapingDownload Octoparse
Scrape posts from RedditWednesday, December 04, 2019
The latest version for this tutorial is available here. Go to have a check now!
In this tutorial, we are going to show you how to scrape posts from a Reddit group.
To follow through, you may want to use this URL in the tutorial:
We will open every post and scrape the data including the group name, author, title, article, the number of the upvote and that of the comments.
This tutorial will also cover:
· Handle pagination empowered by scrolling down in Octoparse
· Deal with AJAX for opening every Reddit post
· Locate all the posts by modifying the loop mode and XPath in Octoparse
Here are the main steps in this tutorial: [Download task file here ]
1) Go To Web Page - to open the targeted web page
· Click "+ Task" to start a task using Advanced Mode
Advanced Mode is a highly flexible and powerful web scraping mode. For people who want to scrape from websites with complex structures, like Airbnb.com, we strongly recommend Advanced Mode to start your data extraction project.
· Paste the URL into the "Extraction URL" box and click "Save URL" to move on
2) Set Scroll Down - to load all items from one page
· Turn on the "Workflow Mode" by switching the "Workflow" button in the top-right corner in Octoparse
We strongly suggest you turn on the "Workflow Mode" to get a better picture of what you are doing with your task, just in case you mess up with the steps.
· Set up Scroll Down
For some websites like Reddit.com, clicking the next page button to paginate is not an option for loading content. To fully load the posts, we need to scroll the page down to the bottom continuously.
· Check the box for "Scroll down to bottom of the page when finished loading"
· Set up "Scroll times", "Interval", and "Scroll way"
By inputting value X into the "Scroll times" box, Octoparse will automatically scroll the page down to the bottom for X times. In this tutorial, 1 is inputted for demonstration purposes. When setting up "Scroll times", you’ll often need to test running the task to see if you have assigned enough times.
"Interval" is the time interval between every two scrolls. In this case, we are going to set "Interval" as 3 seconds.
For "Scroll way", select "Scroll down to the bottom of the page"
· Click "OK" to save
To learn more about how to deal with infinite scrolling in Octoparse, please refer to：
3) Create a "Loop Item" - to loop click into each item on each list
· Select the first three posts on the current page
· Click "Loop click each element" to create a "Loop Item"
Octoparse will automatically select all the posts on the current page. The selected posts will be highlighted in green with other posts highlighted in red.
· Set up AJAX Load for the "Click Item" action
Reddit applies the AJAX technique to display the post content and comments thread. Therefore, we need to set up AJAX Load for the "Click Item" step.
· Uncheck the box for "Retry when page remains unchanged (use discreetly for AJAX loading)" and "Open the link in new tab"
· Check the box for "Load the page with AJAX" and set up AJAX Timeout (2-4 seconds will work usually)
· Click "OK" to save
For more about dealing with AJAX in Octoparse:
4) Extract data - to select the data for extraction
After you click "Loop click each element", Octoparse will open the first post.
· Click on the data you need on the page
· Select "Extract text of the selected element" from "Action Tips"
· Rename the fields by selecting from the pre-defined list or inputting on your own
5) Customize data field by modifying XPath - to improve the accuracy of the item list (Optional)
Once we click “Loop click each element”, Octoparse will generate a loop item using Fixed list loop mode by default. Fixed list is a loop mode used for dealing with a fixed amount of elements. However, the number of posts on Reddit.com is not fixed but increases with scrolling down. In order to enable Octoparse to capture all the posts, including those to be loaded later, we need to swift the loop mode to Variable list and enter the proper XPath to have all the posts to be located.
· Select "Loop Item" box
· Select "Variable list" and enter "//div[contains(@class, "scrollerItem") and not(contains(@class, "promote"))]"
· Click "OK" to save
1. "Fixed list" and "Variable list" are loop modes in Octoparse. For more about loop modes in Octoparse:
2. If you want to learn more about XPath and how to generate it, here is a related tutorial you might need：
6) Start extraction - to run the task and get data
· Click “Start Extraction” on the upper left side
· Select “Local Extraction” to run the task on your computer, or select “Cloud Extraction” to run the task in the Cloud (for premium users only)
Here is the sample output.
Was this article helpful? Feel free to let us know if you have any question or need our assistance.
Contact us here !
- Most popular tutorials
- Scrape data on Instagram
- Scrape business information from Yelp
- Scrape tweets from Twitter
- Scrape listings from Airbnb
- Scrape hotel data from Booking.com