Python) Save scraping content to local PC

Beginners in programming are learning scraping. For the time being, the scraped contents can be downloaded to a local file, so make a note as a memorandum before forgetting.

#Sample for scraping articles and URLs from websites and downloading results to local PC

#Import required modules
from bs4 import BeautifulSoup
import requests
import pandas as pd

#Create a data frame
columns = ["Article title", "URL"]
df = pd.DataFrame(columns = columns)

#Get the content from the website with requests and process the content with Beautiful Soup
res = requests.get("https:~~ URL of the website you want to scrape ~~")
soup = BeautifulSoup(res.content, 'html.parser') #Beautiful Soup initialization
tags = soup.find_all("XXXXX", {"class": "YYYYYY"}) #X and Y vary depending on the website

#Add article name and URL to dataframe
for tag in tags:
 article = tag.a.string
 url = tag.a.get("href")
 se = pd.Series([article, url], columns)
 df = df.append(se, columns)

#「to_Save the csv file in the same folder where the code is saved using "csv"
df.to_csv("./news.csv")
print("End")

Recommended Posts

Python) Save scraping content to local PC
[Python] Until scraping beginners save J-League standings to CSV files
[Scraping] Python scraping
Function to save images by date [python3]
[Part1] Scraping with Python → Organize to csv!
What I did to save Python memory
Python scraping notes
Updated to Python 2.7.9
Scraping tabelog with python and outputting to CSV
Strategy to bring local scraping work to GCP (Part 1)
Python Scraping eBay
[Python] Flow from web scraping to data analysis
Python Scraping get_title
Python: Scraping Part 1
I want to sell Mercari by scraping python
Scraping using Python
"Backport" to python 2
Python: Scraping Part 2
[Python] Local → Procedure for uploading files to S3 (boto3)
I was addicted to scraping with Selenium (+ Python) in 2020
Save lists, dictionaries and tuples to external files python
Install Python environment on local PC (pyenv, venv on Mac)
Scraping with Python (preparation)
Summary about Python scraping
Try scraping with Python.
Changes from Python 3.0 to Python 3.5
UnicodeEncodeError:'cp932' during python scraping
Basics of Python scraping basics
Scraping with Python + PhantomJS
Introduction to Web Scraping
How to install python
python decorator to retry
Introduction to Python language
Scraping: Save website locally
Note to daemonize python
Introducing Python 2.7 to CentOS 6.6
Scraping with Selenium [Python]
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
Python web scraping selenium
Scraping with Python + PyQuery
Local server with python
Scraping RSS with Python
Library path setting to pass GAE / Python local unit tests
Create a tool to check scraping rules (robots.txt) in Python
Save images on the web to Drive with Python (Colab)