WEB scraping with Python (for personal notes)

On the second day after I started learning Python by myself, I tried simple web scraping. I made it with reference to some sites. Post it for personal notes. This time ** ・ title ** ** ・ h2 ** Extract two elements.

Preparation

-Install the requests module

command


pip install requests

-Install the Beautiful Soup module

command


pip install beautifulsoup4

program

code


import requests
from bs4 import BeautifulSoup

#URL you want to get
url = "*********"

#Get HTML by sending HTTP request with url as argument
response = requests.get(url)

#Character code is automatically encoded
response.encoding = response.apparent_encoding

#HTML parsing
bs = BeautifulSoup(response.text, 'html.parser')
#Extract title
title_tag = bs.find('title')
print(title_tag.text)
#Extract h2 element
h2_tags = bs.select('h2')
for h2_tag in h2_tags:
    print(h2_tag.text)

Reference site

Always work next to IT

Recommended Posts

WEB scraping with Python (for personal notes)
[Personal note] Web page scraping with python3
[For beginners] Try web scraping with Python
Web scraping with python + JupyterLab
Web scraping notes in python3
Web scraping beginner with python
Data analysis for improving POG 1 ~ Web scraping with Python ~
Web scraping with Python ① (Scraping prior knowledge)
Web scraping with Python First step
I tried web scraping with python.
Personal notes for python image processing
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
Python scraping notes
Scraping with Python
Scraping with Python
python personal notes
Getting Started with Python Web Scraping Practice
Web scraping with Python ② (Actually scraping stock sites)
Horse Racing Site Web Scraping with Python
Getting Started with Python Web Scraping Practice
Practice web scraping with Python and Selenium
Easy web scraping with Python and Ruby
Preparation for scraping with python [Chocolate flavor]
Scraping with Python (preparation)
Try scraping with Python.
missingintegers python personal notes
Scraping with Python + PhantomJS
[Personal notes] Python, Django
Scraping with Selenium [Python]
Python web scraping selenium
Scraping with Python + PyQuery
Scraping RSS with Python
AWS-Perform web scraping regularly with Lambda + Python + Cron
Let's do web scraping with Python (weather forecast)
Let's do web scraping with Python (stock price)
I tried scraping with Python
Beginners can use Python for web scraping (1) Improved version
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Scraping with chromedriver in python
Festive scraping with Python, scrapy
Save images with web scraping
Manipulate excel files from python with xlrd (personal notes)
Python Tkinter notes (for myself)
Scraping with Selenium in Python
Quick web scraping with Python (while supporting JavaScript loading)
Easy web scraping with Scrapy
Scraping with Tor in Python
Web API with Python + Falcon
Python beginners get stuck with their first web scraping
Web scraping using Selenium (Python)
Scraping weather forecast with python
Scraping with Selenium + Python Part 2
Web application with Python + Flask ② ③
[Python + Selenium] Tips for scraping
I tried scraping with python
Analyze Amazon Gift Certificate Low Price Information with Python for Web Scraping & R
Streamline web search with python
Beginners use Python for web scraping (4) --2 Scraping on Cloud Shell
Web application with Python + Flask ④