The Wayback Machine - https://web.archive.org/web/20241128075741/https://www.geeksforgeeks.org/scraping-reddit-with-python-and-beautifulsoup/
Open In App

Scraping Reddit with Python and BeautifulSoup

Last Updated : 21 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we are going to see how to scrape Reddit with Python and BeautifulSoup. Here we will use Beautiful Soup and the request module to scrape the data.

Module needed

  • bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install this type the below command in the terminal.
pip install bs4
  • requests: Request allows you to send HTTP/1.1 requests extremely easily. This module also does not come built-in with Python. To install this type the below command in the terminal.
pip install requests

Approach:

  • Import all the required modules.
  • Pass the URL in the getdata function(UDF) to that will request to a URL, it returns a response. We are using the GET method to retrieve information from the given server using a given URL.

Syntax: requests.get(url, args)

  • Now Parse the HTML content using bs4.

Syntax: soup = BeautifulSoup(r.content, ‘html5lib’)

Parameters:

  • r.content : It is the raw HTML content.
  • html.parser : Specifying the HTML parser we want to use.
  • Now filter the required data using soup.Find_all function.

Let’s see the stepwise execution of the script.

Step 1: Import all dependence

Python3




# import module
import requests
from bs4 import BeautifulSoup


Step 2: Create a URL get function

Python3




# user define function
# Scrape the data
def getdata(url):
    r = requests.get(url, headers = HEADERS)
    return r.text


Step 3: Now take the URL and pass the URL into the getdata() function and Convert that data into HTML code.

Python3




 
# pass the url
# into getdata function
htmldata = getdata(url)
soup = BeautifulSoup(htmldata, 'html.parser')
   
# display html code
print(soup)


Output:

Note: This is only HTML code or Raw data.

Getting Author Name

Now find authors with a div tag where class_ =”NAURX0ARMmhJ5eqxQrlQW”. We can open the webpage in the browser and inspect the relevant element by pressing right-click as shown in the figure.

Example:

Python3




# find the Html tag
# with find()
# and convert into string
data_str = ""
for item in soup.find_all("div", class_="NAURX0ARMmhJ5eqxQrlQW"):
    data_str = data_str + item.get_text()
       
print(data_str)


Output:

kashaziz

Getting article contains

Now find the article text, here we will follow the same methods as the above example.

Example:

Python3




# find the Html tag
# with find()
# and convert into string
data_str = ""
result = ""
for item in soup.find_all("div", class_="_3xX726aBn29LDbsDtzr_6E _1Ap4F5maDtT1E1YuCiaO0r D3IL3FD0RFy_mkKLPwL4"):
    data_str = data_str + item.get_text()
print(data_str)


Output:

Getting the comments

Now Scrape the comments, here we will follow the same methods as the above example.

Python3




# find the Html tag
# with find()
# and convert into string
data_str = ""
 
for item in soup.find_all("p", class_="_1qeIAgB0cPwnLhDF9XSiJM"):
    data_str = data_str + item.get_text()
print(data_str)


Output:



Previous Article
Next Article

Similar Reads

Scraping Reddit using Python
In this article, we are going to see how to scrape Reddit using Python, here we will be using python's PRAW (Python Reddit API Wrapper) module to scrape the data. Praw is an acronym Python Reddit API wrapper, it allows Reddit API through Python scripts. Installation To install PRAW, run the following commands on the command prompt: pip install praw
4 min read
BeautifulSoup object - Python Beautifulsoup
BeautifulSoup object is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. The BeautifulSoup object represents the parsed document as a whole. For most purposes, you can treat it as a Tag object. Syntax: BeautifulS
2 min read
Implementing Web Scraping in Python with BeautifulSoup
There are mainly two ways to extract data from a website: Use the API of the website (if it exists). For example, Facebook has the Facebook Graph API which allows retrieval of data posted on Facebook.Access the HTML of the webpage and extract useful information/data from it. This technique is called web scraping or web harvesting or web data extrac
8 min read
Web Scraping using Beautifulsoup and scrapingdog API
In this post we are going to scrape dynamic websites that use JavaScript libraries like React.js, Vue.js, Angular.js, etc you have to put extra efforts. It is an easy but lengthy process if you are going to install all the libraries like Selenium, Puppeteer, and headerless browsers like Phantom.js. But, we have a tool that can handle all this load
5 min read
BeautifulSoup - Scraping List from HTML
Prerequisite: RequestsBeautifulSoup Python can be employed to scrap information from a web page. It can also be used to retrieve data provided within a specific tag, this article how list elements can be scraped from HTML. Module Needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not co
2 min read
BeautifulSoup - Scraping Paragraphs from HTML
In this article, we will discuss how to scrap paragraphs from HTML using Beautiful Soup Method 1: using bs4 and urllib. Module Needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. For installing the module-pip install bs4.urllib: urllib is a package that collects several modules for working with URLs. It
3 min read
BeautifulSoup - Scraping Link from HTML
Prerequisite: Implementing Web Scraping in Python with BeautifulSoup In this article, we will understand how we can extract all the links from a URL or an HTML document using Python. Libraries Required:bs4 (BeautifulSoup): It is a library in python which makes it easy to scrape information from web pages, and helps in extracting the data from HTML
2 min read
Python - Making a Reddit bot with PRAW
Reddit is a network of communities based on people’s interests. Each of these communities is called a subreddit. Users can subscribe to multiple subreddits to post, comment and interact with them. A Reddit bot is something that automatically responds to a user’s post or automatically posts things at certain intervals. This could depend on what cont
2 min read
Python PRAW - Checking whether a redditor is an employee of Reddit or not
In Reddit, a redditor is the term given to a user. Here we will see how to check whether a redditor is an employee of Reddit or not. We will be using the is_employee attribute of the Redditor class to check whether a redditor is an employee of Reddit or not. Example 1 : Consider the following redditor : The user name of the redditor is : spez, spez
2 min read
Python PRAW – Check whether a redditor has Reddit premium or not
In Reddit, a redditor is the term given to a user. Reddit allows redditors to avail premium access by paying a fee. Here we will see how to check whether a redditor has the premium access or not. We will be using the is_gold attribute of the Redditor class to check whether a redditor has the premium access or not. Example 1 : Consider the following
2 min read
Python PRAW - Getting the ID of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Reddit assigns each and every comment with an identification. Here we will see how to fetch the ID of a comment using PRAW. We will be using the id attribute of the Comment class to fetch the ID of a comment. Example 1 : Consider the
2 min read
Python PRAW - Getting the body of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Every comment have some textual information in it which is called the body of the comment. Here we will see how to fetch the body of a comment using PRAW. We will be using the body attribute of the Comment class to fetch the body of
2 min read
Python PRAW - Checking whether a commenter is also the author of the submission in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. The author of a submission can also later comment on their own submission. Here we will see how to check whether a comment author is also the author of the submission using PRAW. We will be using the is_submitter attribute of the Com
2 min read
Python PRAW - Checking whether a comment has been edited or not in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. A comment can be later edited after posting it. Here we will see how to check whether a comment has been edited or not using PRAW. We will be using the edited attribute of the Comment class to check whether a comment has been edited
2 min read
Python PRAW - Getting the time when a comment was posted on Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the exact time when a comment was posted using PRAW. We will be using the created_utc attribute of the Comment class to fetch the Unix time when the comment was posted. Example 1 : Consider the following
2 min read
Python PRAW - Checking whether a comment is distinguished or not in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. A moderator of a subreddit can mark any comment as distinguished. Here we will see how to check whether a comment is distinguished or not using PRAW. We will be using the distinguished attribute of the Comment class to check whether
2 min read
Python PRAW - Getting the permalink of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Each comment has a permanent static hyperlink affiliated to it. Here we will see how to fetch the permalink of a comment using PRAW. We will be using the permalink attribute of the Comment class to fetch the permalink of a comment. E
2 min read
Python PRAW - Getting the parent ID of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the parent ID of a comment using PRAW. We will be using the is_submitter attribute of the Comment class to fetch the parent ID of a comment. If the comment is top-level then the ID of the submission is f
2 min read
Python PRAW - Getting the ID of the submission that a comment belongs to in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. A comment is always posted on a submission. Here we will see how to fetch the ID of the submission that a comment belongs to using PRAW. We will be using the is_submitter attribute of the Comment class to fetch the ID of the submissi
2 min read
Python PRAW - Checking whether a comment is stickied or not in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. A moderator of a subreddit can mark any comment as stickied. Here we will see how to check whether a comment is stickied or not using PRAW. We will be using the stickied attribute of the Comment class to check whether a comment is st
2 min read
Python PRAW - Getting the score of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Any user can either upvote or downvote a comment, the net value is the score of the comment. Here we will see how to fetch the score of a comment using PRAW. We will be using the score attribute of the Comment class to fetch the scor
2 min read
Python PRAW - Getting the subreddit on which a comment is posted in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the subreddit in which the comment has been posted using PRAW. We will be using the subreddit attribute of the Comment class to fetch subreddit class of the subreddit in which the comment has been posted
2 min read
Python PRAW - Getting the author of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the author of a comment using PRAW. We will be using the author attribute of the Comment class to fetch the Redditor class of the author of a comment. Example 1 : Consider the following comment : The ID
2 min read
Python PRAW - Getting the subreddit ID that a comment belongs to in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the subreddit ID of the subreddit that a comment belongs to using PRAW. We will be using the subreddit_id attribute of the Comment class to fetch the subreddit ID of the subreddit that a comment belongs
2 min read
Python PRAW - Downvoting a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. We can either upvote or downvote a comment. Here we will see how to downvote a comment using PRAW. We will be using the downvote() method of the Comment class to downvote a comment. downvote() Syntax : downvote() Parameters : None Re
2 min read
Python PRAW - Upvoting a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. We can either upvote or downvote a comment. Here we will see how to upvote a comment using PRAW. We will be using the upvote() method of the Comment class to upvote a comment. upvote() Syntax : upvote() Parameters : None Returns : No
2 min read
Python PRAW - Clearing the vote of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. We can either upvote or downvote a comment. Here we will see how to remove either the upvote or the downvote from a comment using PRAW. We will be using the clear_vote() method of the Comment class to clear the vote of a comment. cle
2 min read
Python - Obtain title, views and likes of YouTube video using BeautifulSoup
In this article, we will learn how can we obtain data (like title, views, likes, dislikes etc) from any YouTube video using a Python script. For this task, we are going to use very famous library for web scraping BeautifulSoup and Requests. Modules required and Installation : Requests : Requests allows you to send HTTP/1.1 requests extremely easily
2 min read
Downloading PDFs with Python using Requests and BeautifulSoup
BeautifulSoup object is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. The BeautifulSoup object represents the parsed document as a whole. For most purposes, you can treat it as a Tag object. Requests library i
2 min read
Difference between find and find_all in BeautifulSoup - Python
BeautifulSoup is one of the most common libraries in Python which is used for navigating, searching, and pulling out data from HTML or XML webpages. The most common methods used for finding anything on the webpage are find() and find_all(). However, there is a slight difference between these two, let's discuss them in detail. find() method The find
3 min read
three90RightbarBannerImg