0
0

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 3 years have passed since last update.

Pythonで初めてwebスクレイピングしてみる

Posted at

この記事で行うこと

じゃらんの口コミをスクレイピングで取得し、ローカルのエクセルにcsv形式で出力・保存。
スクレイピング対象のURLは下記になります。
https://www.jalan.net/yad330474/kuchikomi/?screenId=UWW3001&yadNo=330474&smlCd=012102&distCd=01

環境

Python3.9
windows10

jupyter notebookのセットアップ

①jupyterをインストール

pip install jupyter

②requestsをインストール

pip install requests

③pandasをインストール

pip install pandas

④beautiful soup4をインストール

pip install bs

⑤jupyter notebook起動

python -m notebook

⑥プルダウンからPython3を選択

image.png

⑦コードを記述&実行

コードを記述し、Alt + Enterまたは実行ボタンで実行するとjupyter上で出力及びローカルにエクセルが出来ています。
今回出力形式が日本語のため、文字化け解消にutf_8_sigを記述しています。
image.png

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.jalan.net/yad330474/kuchikomi/?screenId=UWW3001&yadNo=330474&smlCd=012102&distCd=01")
c = r.content

soup = BeautifulSoup(c, "html.parser")

all=soup.find_all("p",{"class":"jlnpc-kuchikomiCassette__postBody"})

l=[]
for item in all:
    d={}
    d["クチコミ"]=item.text
    l.append(d)

import pandas
df=pandas.DataFrame(l)
df.to_csv("じゃらん.csv", encoding='utf_8_sig')
df

image.png

0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?