make web scraper in python

To create a web scraper in Python, you can use the requests library to send HTTP requests to a webpage and obtain the HTML content. After that, you can use beautifulsoup library to parse the HTML content and extract the information you want.

Here is an example code to scrape the titles of the top posts from the front page of Reddit using Python:

main.py
import requests
from bs4 import BeautifulSoup

url = "https://www.reddit.com/"

response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
post_titles = soup.find_all("h3", class_="s1y1r4hx-2")

for post_title in post_titles:
    print(post_title.text)
274 chars
12 lines

In this code, we first import the requests and beautifulsoup libraries. Then, we define the URL of the webpage we want to scrape and use requests.get() method to send an HTTP request to that URL and get the HTML content. We then create a BeautifulSoup object by passing the HTML content as the first argument and the string "html.parser" as the second argument (which tells BeautifulSoup to use the HTML parser).

To extract the titles of the top posts, we use the soup.find_all() method to find all the <h3> elements with the class name "s1y1r4hx-2" (which corresponds to the post titles on Reddit). We then loop through the list of post titles and print out their text contents.

This is just a basic example, but you can use similar techniques to scrape more complex websites and extract different types of information. Just be sure to read and follow the website's terms of service and robots.txt file to avoid any legal issues.

gistlibby LogSnag