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

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

新規登録して質問してみよう
ただいま回答率
85.48%
スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Beautiful Soup

Beautiful Soupは、Pythonのライブラリの一つ。スクレイピングに特化しています。HTMLデータの構文の解析を行うために、HTMLタグ/CSSのセレクタで抽出する部分を指定することが可能です。

iframe

HTMLのタグ<iframe>です。<iframe>は、ドキュメント内に""inline frame""を作るHTML要素で、同じページでセパレートしているドキュメントが表示されるようにします。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

1回答

2296閲覧

pythonスクレイピング時にiframe要素を一つしか抽出できないのですが、複数抽出したいです。

ryuryukiki

総合スコア6

スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Beautiful Soup

Beautiful Soupは、Pythonのライブラリの一つ。スクレイピングに特化しています。HTMLデータの構文の解析を行うために、HTMLタグ/CSSのセレクタで抽出する部分を指定することが可能です。

iframe

HTMLのタグ<iframe>です。<iframe>は、ドキュメント内に""inline frame""を作るHTML要素で、同じページでセパレートしているドキュメントが表示されるようにします。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2019/09/09 14:53

編集2019/09/09 14:56

前提・実現したいこと

python初心者です。
python3,beautifulsoup,seleniumで練習でwebスクレイピングをしています。

<iframe>内にあるInstagramの投稿のsrcの中身を抽出したいのですが、1ページ内に複数<iframe>タグがある場合、一つ目の<iframe>のsrcしか抽出できません。

1ページ内にある全ての<iframe>タグを抽出したいです。

発生している問題・エラーメッセージ

for文で回しているのですが、<iframe>のsrcを一つしか取れません

取得しているページは
https://www.buzzfeed.com/jp/stephenlaconte/kylie-jenner-birthday-vacation-1?origin=thum
です。

どうかよろしくお願いいたします。

エラーメッセージ

該当のソースコード

python3

1import requests 2from bs4 import BeautifulSoup 3from selenium import webdriver 4import chromedriver_binary 5from selenium.webdriver.chrome.options import Options 6from selenium.common.exceptions import TimeoutException 7from selenium.webdriver.support.ui import WebDriverWait 8from selenium.webdriver.support import expected_conditions as EC 9from selenium.webdriver.common.by import By 10import time 11 12options = Options() 13options.set_headless(True) 14 15driver = webdriver.Chrome() 16driver.get("https://www.buzzfeed.com/jp/stephenlaconte/kylie-jenner-birthday-vacation-1?origin=thum") 17html = driver.page_source.encode('utf-8') 18 19html01=driver.page_source 20while 1: 21 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 22 time.sleep(3) 23 html02=driver.page_source 24 if html01!=html02: 25 html01=html02 26 driver.implicitly_wait(4) 27 else: 28 break 29 30soup2 = BeautifulSoup(html, "lxml") 31 32 33contentList = [] 34 35 36for div in soup2.find_all("div",class_ = "buzz-article-wrapper js-buzz-article-wrapper"): 37# if div.find("div",class_ ="action-bar-wrapper js-action-bar-wrapper"): 38# div.find("div",class_ ="action-bar-wrapper js-action-bar-wrapper").decompose() 39# if div.find("ul",class_ ="action-bar action-bar--subbuzz js-action-bar--subbuzz list-unstyled"): 40# div.find("ul",class_ ="action-bar action-bar--subbuzz js-action-bar--subbuzz list-unstyled").decompose() 41# if div.find("div",class_ ="subbuzz-quiz"): 42# div.find("div",class_ ="subbuzz-quiz").decompose() 43# if div.find("div",class_ ="js-unsupported-fallback subbuzz"): 44# div.find("div",class_ ="js-unsupported-fallback subbuzz").decompose() 45# if div.find("div",class_ ="subbuzz-bfp"): 46# div.find("div",class_ ="subbuzz-bfp").decompose() 47# if div.find("div",class_ ="subbuzz-youtube"): 48# div.find("div",class_ ="subbuzz-youtube").decompose() 49# if div.find("div",class_ ="subbuzz-photoslider"): 50# div.find("div",class_ ="subbuzz-photoslider").decompose() 51# if div.find("div",class_ ="subbuzz-facebook"): 52# div.find("div",class_ ="subbuzz-facebook").decompose() 53# if div.find("div",class_ ="subbuzz-freeform"): 54# div.find("div",class_ ="subbuzz-freeform").decompose() 55# if div.find("div",class_ ="action-bar-wrapper action-bar-wrapper--bottom js-action-bar-wrapper"): 56# div.find("div",class_ ="action-bar-wrapper action-bar-wrapper--bottom js-action-bar-wrapper").decompose() 57# 58# 59 for content in div.find_all(["p","h2","a","img","iframe"]): 60# if div.find("img", class_ = "subbuzz__media-image",alt ="Instagram"): 61# div.find("img",class_ = "subbuzz__media-image", alt ="Instagram").decompose() 62# if div.find("div", class_ = "subbuzz__media-cta xs-absolute xs-b0 xs-col-12 xs-p2 xs-text-center text-gray-lighter"): 63# div.find("div",class_ = "subbuzz__media-cta xs-absolute xs-b0 xs-col-12 xs-p2 xs-text-center text-gray-lighter").decompose() 64# if div.find("div", class_ = "ad-awareness-wrapper js-awareness-wrapper"): 65# div.find("div",class_ = "ad-awareness-wrapper js-awareness-wrapper").decompose() 66# if div.find("div", class_ = "clearfix js-ad-placement ad-inline"): 67# div.find("div",class_ = "clearfix js-ad-placement ad-inline").decompose() 68 69 if content.name == "p": 70 if div.find("div",class_ ="text-white"): 71 div.find("div",class_ ="text-white").decompose() 72 texts = content.text 73 print(texts) 74 contentList.append(texts.replace("\u3000","").strip()) 75 if content.name == "h2": 76 texth2 = content.text 77 print(texth2) 78 contentList.append(texth2.replace("\u3000","").strip()) 79 if content.name == "a": 80 if div.find("ul",class_ ="action-bar action-bar--subbuzz js-action-bar--subbuzz list-unstyled"): 81 div.find("ul",class_ ="action-bar action-bar--subbuzz js-action-bar--subbuzz list-unstyled").decompose() 82 texth3 = content.text 83 print(texth3) 84 contentList.append(texth3.replace("\u3000","").strip()) 85 if content.name == "img": 86 if div.find("img", alt ="Instagram"): 87 div.find("img", alt="Instagram").decompose() 88 if div.find("noscript"): 89 div.find("noscript").decompose() 90 if content.get("data-src"): 91 print(content.get("data-src")) 92 contentList.append(content.get("data-src").strip()) 93 else: 94 print(content.get("src")) 95 contentList.append(content.get("src").strip()) 96 if content.name == "iframe": 97 content.get("src") 98 print(content.get("src")) 99 contentList.append(content.get("src")) 100driver.close() 101print("----------------------------")

試したこと

一番下までスクロールさせてページを読み込んでから要素を取ろうとしましたが変わりませんでした。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

meg_

2019/09/09 21:57

「html」の中に取得したいものは含まれていますか?
t_obara

2019/09/10 01:28

selenium であれば、driver.find_elements_by_tag_nameでiframeを指定する感じ
guest

回答1

0

ベストアンサー

python

1for iframe in driver.find_elements_by_tag_name('iframe'): 2 print(iframe.get_attribute('src'))

投稿2019/09/10 02:58

shirai

総合スコア1290

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問