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

スクレイピングでタグから店舗名と住所を取得

0
Last updated at Posted at 2019-01-28

[やりたいこと]
喫煙可能な喫茶店のリスト(店舗名と住所)を作成したい。

今回はよくお世話になるドトールさんの店舗リストからスクレイピングし、
喫煙席を有する店舗をピックアップする手法を考える(分煙アイコンや各店舗に入れ子で入って情報取得)

しかしドトールはほとんど喫煙可能(分煙対応)なためシンプルに東京の店舗名をすべて取得することに変更

想定手法
・find_all()でタグの中身(店舗名、住所)を一括取得

手こずった点
・find_allだと中身のテキストをstringでとれない。find()でひとつずつであればfind.stringでとれるが。
小一時間苦戦
・find_all()で取得し、テキストを加工することにした
 Noneとなってエラーが生じる⇒.stringだとタグ取得はNoneとして処理されるため.textで処理する。

使用したもの
・BeautifulSoup
・requests
・find_all,strip
・zip,enumerate

店舗の名前と住所のタグを確認

<dt><span></span><br /><a href="/b/doutor/info/01010009/?shopmastergyokna=1100">ドトールコーヒーショップ 神田西口店</a></dt>
<dd class="MapiInfoAddr">東京都千代田区内神田1‐17 小堀ビル1F</dd>

欲しいデータはddタグとaタグの中身であることを確認

url = "http://sasp.mapion.co.jp/b/doutor/attr/?t=attr_con&bunen=1&kencode=13&shopmastergyokna=1100"

response=requests.get(url)
res = BeautifulSoup(response.text,"html.parser")
res_ad=res.find_all("dd",class_="MapiInfoAddr")
タグの中のMapiInfoAddr classをすべて取得
adresses=[]
for re in res_ad:
    re=re.text
    #textで処理しないとタグで取得しているためNoneになる
    re=re.strip("<dclas=\"MpiInfoAr>")
    adresses.append(re)

.textにしないとstrip()で加工ができない。
.stringにするとNone判定となる

res_na=res.find_all("a")
for i in res_na:
    title=i.string
    titles.append(title)
titles=titles[1:24]    

cafe=[]
for i,(name,address) in enumerate(zip(titles,adresses),1):
    id=(i,name,address)
    ids.append(id)

これでid番号のついた店舗名と住所のリストが作成できた

0
0
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
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?