質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1387閲覧

Python3のスクレイピング(BeautifulSoup)を使用してhref属性の情報を取得したい

nobsxxt

総合スコア8

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2021/04/06 12:00

Pythonのスクレイピングの勉強をしておりまして、タウンページの検索結果(動物病院 東京都中野区)を取得してCSVに書き出したいです。
病院名やカテゴリー、住所などのテキストは取得できるのですが、「ウェブサイト」というaタグのhref属性が取得できません。
※現状は「ウェブサイト」というテキストを取得しております。

URLを取得するにはどのようなコードにすれば良いかご教授いただきたいです...!
どうぞよろしくお願いいたします!

Version
Python 3.8.1
beautifulsoup4 4.9.3
chromedriver-binary 89.0.4389.23.0
idna 2.10
numpy 1.20.2
pip 21.0.1
requests 2.25.1
selenium 3.141.0

Code

import csv import numpy as np import requests from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Options import chromedriver_binary import itertools import collections from collections import Iterable from time import sleep options = Options() options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=options) driver.get("https://itp.ne.jp/topic/?topic=199&area=13114&sort=01&sbmap=false") while True: try: driver.find_element_by_class_name('m-read-more__text').click() except: print('---------------「さらに表示」押下完了---------------') break r = driver.page_source soup = BeautifulSoup(r, "html.parser") shop_name = soup.find_all("a","m-article-card__header__title__link") name_list = [] for name in shop_name: name_list.append(name.get_text().strip()) shop_cg_header= soup.find_all("header", "m-article-card__header") cg_list = [] for header in shop_cg_header: each_cg = header.find("p", "m-article-card__header__category") if each_cg == None: cg_list.append("") else: cg_list.append(each_cg.get_text().strip()) print(cg_list) print(len(cg_list)) shop_web= soup.find_all("div", "m-article-card__tag") web_list = [] for web in shop_web: web_list.append(web.get_text().strip()) shop_cap = soup.find_all("div", "m-article-card__lead__body") cap_list = [] for cap in shop_cap: cap_each = cap.find_all("p") cap_list_each = [] for each in cap_each: cap_list_each.append(each.get_text().strip()) cap_list.append(cap_list_each) cap_list_size = [len(v) for v in cap_list] final_list = [] for i in range(len(name_list)): name = [] name.append(name_list[i]) cg = [] cg.append(cg_list[i]) web = [] web.append(web_list[i]) cap = [] cap.append(cap_list[i]) list = name + cg + web + cap final_list.append(list) def flatten(x): result = [] for el in x: if isinstance(x, collections.Iterable) and not isinstance(el, str): result.extend(flatten(el)) else: result.append(el) return result clean_data =[] for i in range(len(final_list)): list = flatten(final_list[i]) clean_data.append(list) header = ["No.","会社名", "カテゴリー", "Web", "特徴", "最寄駅", "Tel", "Adrress"] index = [] for i in range(len(clean_data)): index.append(i+1) with open('動物病院.csv', 'w', newline="") as f: writer = csv.writer(f) writer.writerow(header) for i, row in zip(index, clean_data): writer.writerow([i] + row)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

一つ目の値を取得する例です。

python

1import requests 2from bs4 import BeautifulSoup 3 4res = requests.get('https://itp.ne.jp/topic/?topic=199&area=13114&sort=01&sbmap=false') 5soup = BeautifulSoup(res.text) 6print(soup.select('.m-card-tag-button')[0].get('href')) 7# 'http://www.ohmura-ah.com/'

禁止事項に自動的にアクセスするプログラムを使用してiタウンページに繰り返しアクセスする行為がありますので繰り返しアクセスしないようにご注意ください。

投稿2021/04/06 12:21

meg_

総合スコア10580

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nobsxxt

2021/04/07 12:10

hrefを取得することができました!ありがとうございます!! 禁止事項だったとは知らずにすみません。 今後はきちんと規約を確認するようにいたします。
meg_

2021/04/07 12:19

短時間に頻繁にアクセスしなければ大丈夫だと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問