0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WEBスクレイピング

Posted at

Pythonで東京都にあるカーディーラーの某WEBサイトをスクレイピング。
865店舗の店舗名と住所をスクレイピングしてcsvファイルに保存。
生成したcsvファイルの活用編は次回!

※スクレイピングが禁止されているサイトもあるので利用規約を要確認。
WEBサイトに負荷が掛かる場合があるので要time.sleep

import requests
from bs4 import BeautifulSoup

base_url = "WEBサイトのURL"

url = base_url
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
soup

#soupでHTMLを表示して抽出したいタグを確認
#今回は店舗名と住所をスクレイピング
---------------------------------------------------------------------------
import requests
from bs4 import BeautifulSoup
import csv

base_url = "WEBサイトのURL(ページナンバーは削除)"
total_pages = 29 #全部で29ページ

with open('生成するファイル名.csv', 'w', encoding='shift_jis') as csv_file:
    csv_writer = csv.writer(csv_file)

    for page_number in range(1, total_pages + 1):
        url = base_url + str(page_number)

         response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')

       for shop_info in soup.find_all(""抽出する要素"):
             shop_name = shop_info.find("店舗名の要素").text

            address = shop_info.find("住所の要素").text

            csv_writer.writerow([shop_name, address])

キャプチャ.PNG

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?