LoginSignup
0
1

More than 1 year has passed since last update.

【一分理解】スクレイピンングしてみよう

Last updated at Posted at 2022-03-23

スクレイピングってなに?

スクレイピングとは、ウェブサイトから情報を抽出することです。
サイト上のデータ(商品、価格、天気などのデータ)を取ってくることが多いです。

pythonで書いてみよう

必要なもの(pip)

・requests
・BeautifulSoup
・調べたいサイトのURL

流れ

requestsで取得

res = requests.get("URL")

BeautifulSoupでサイトデータを抽出

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

欲しい部分を決定
ex)クラス名:product_lists aaa の ul の中の liを取得
※商品一覧などの構造はだいたいこんな感じ

found_part = soup.find_all("ul", class_='product_lists aaa')
for ul_tag in found_part:
        for li in ul_tag.find_all('li'):
            print(li.find('span').text)

サンプルソース
MyGitHub

ここ注意

サイトによってはスクレイピングを禁止しているものもあるため、確認する必要がある。

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