I don't know website scraping lingo, but deal mostly with Excel. I'm come to a point where I'm looking to hire someone but I want to ensure I'm speaking the same language. I need to web scrape different outcomes from about 1000 different outcomes on the same site. It's a drop down list and each option have about 5 outcomes and 20 lines of data. Scraping Reddit with Python and BeautifulSoup 4 In this tutorial, you'll learn how to get web pages using requests, analyze web pages in the browser, and extract information from raw HTML with BeautifulSoup. You can find a finished working example of the script we will write here. Welcome to the most interesting (and fun!) blog post on web scraping for dummies. Mind you, this is not a typical web scraping tutorial. You will learn the whys and hows of data scraping along with a few interesting use-cases and fun facts.
Wednesday, December 04, 2019The 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
Tips! To learn more about how to deal with infinite scrolling in Octoparse, please refer to: · Dealing with Infinite Scrolling/Load More |
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'
Scraping Reddit Using Python
· Check the box for 'Load the page with AJAX' and set up AJAX Timeout (2-4 seconds will work usually)
· Click 'OK' to save
Tips! For more about dealing with AJAX in Octoparse: · Deal with AJAX |
4) Extract data - to select the data for extraction
After you click 'Loop click each element', Octoparse will open the first post.
Web Scraping Reddit Comments
· 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
Tips! 1. 'Fixed list' and 'Variable list' are loop modes in Octoparse. For more about loop modes in Octoparse: ·5 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: ·Locate elements with XPath |
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 ushere !