0
1

Visual StudioとPythonでWebスクレイピングを入門してみる

Last updated at Posted at 2024-01-24

概要

Visual StudioでPythonを試してみる で開発環境を作りました。
今回は、もう少しPythonを学習します。
ネタは、Webスクレイピングです。

以下のライブラリを使います。
Requests 2.31.0
Beautiful Soup 4.12.3

ライブラリのインストール

パッケージのインストール手順

1. ソリューションエクスプローラー → Python 環境 → すべてのPython 環境を表示

すべてのPython環境.png

2. Python 環境 → パッケージ(PyPl)

パッケージ.png

3. 検索フィールドにライブラリ名を入力
 → [次のコマンドを実行する: pip install ライブラリ名]を押下する

検索フィールド-入力.png

4. 「今すぐ昇格」を選択

昇格.png

5. 完了

完了.png

Requestsライブラリ

検索フィールドに「Requests」を入力してパッケージのインストール手順 を実施する

Beautiful Soupライブラリ

検索フィールドに「BeautifulSoup4」を入力してパッケージのインストール手順 を実施する

HTMLデータの取得

Yahooのリアルタイム検索を取得してみる

PythonApplication1.py
import requests
url = "https://search.yahoo.co.jp/realtime"
response = requests.get(url)
print(response.text)

実行してみる

HTMLデータの取得.png

HTMLデータの解析と抽出

Yahooのリアルタイム検索のキーワードを抽出してみる

PythonApplication1.py
import requests
from bs4 import BeautifulSoup

url = "https://search.yahoo.co.jp/realtime"
response = requests.get(url)

soup = BeautifulSoup(response.text, "html.parser")

for item in soup.select('article > h1'):
    print(item.text)

実行してみる

実行する.png

実際のページ

Yahooリアルタイム検索.png

参考URL

Pythonで始めるWebスクレイピング入門!
手順 5: Python 環境へのパッケージのインストール

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1