Created
July 29, 2018 12:02
-
-
Save bradtraversy/f2014a236646ff62dccfc9fe5d469ed5 to your computer and use it in GitHub Desktop.
Simple scraping of a blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
from csv import writer | |
response = requests.get('http://codedemos.com/sampleblog/') | |
soup = BeautifulSoup(response.text, 'html.parser') | |
posts = soup.find_all(class_='post-preview') | |
with open('posts.csv', 'w') as csv_file: | |
csv_writer = writer(csv_file) | |
headers = ['Title', 'Link', 'Date'] | |
csv_writer.writerow(headers) | |
for post in posts: | |
title = post.find(class_='post-title').get_text().replace('\n', '') | |
link = post.find('a')['href'] | |
date = post.select('.post-date')[0].get_text() | |
csv_writer.writerow([title, link, date]) |
Thank you Brad for the ultimate awesomeness
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone looking to find good test sites, webscraper.io should do the trick