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

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

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

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

Python

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

selenium

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

Q&A

解決済

1回答

1837閲覧

Seleniumを用いてWebサイトのタイトルおよびURLしたいがエラーがでてしまう

Yuu412

総合スコア37

スクレイピング

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

Python

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

selenium

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

0グッド

0クリップ

投稿2019/10/11 16:09

PythonでSeleniumを用いてYoutubeのタイトルおよびURLしたいのですが、エラーがでてしまいうまくいきません。
エラーではHTMLの指定しているところがおかしいというように書いているのかと思うのですが、見直してみてもどう間違えているのかわかりません。
よろしくお願いします。

以下コード参照(コメントはコピペのため無視してください)

Python

1import time 2from selenium import webdriver 3 4driver = webdriver.Chrome(executable_path=r'C:\Selenium_re\chromedriver.exe') # ブラウザを起動 5get_url = 'https://www.youtube.com/' 6driver.get(get_url) # webページを開く 7time.sleep(2) 8 9elem_search_word = driver.find_element_by_id("search") 10elem_search_word.send_keys("abc") 11elem_search_btn = driver.find_element_by_id("search-icon-legacy") 12elem_search_btn.click() 13time.sleep(5) 14 15def ranking(driver): 16 i = 1 # ループ番号、ページ番号を定義 17 i_max = 2 # 最大何ページまで分析するかを定義 18 title_list = [] # タイトルを格納する空リストを用意 19 link_list = [] # URLを格納する空リストを用意 20 21 # 現在のページが指定した最大分析ページを超えるまでループする 22 while i <= i_max: 23 # タイトルとリンクはclass="r"に入っている 24 class_group = driver.find_elements_by_class_name('style-scope ytd-section-list-renderer') 25 # タイトルとリンクを抽出しリストに追加するforループ 26 for elem in class_group: 27 title_list.append(elem.find_element_by_class_name('yt-simple-endpoint style-scope ytd-video-renderer').text) #タイトル(class="LC20lb") 28 link_list.append(elem.find_element_by_tag_name('a').get_attribute('href')) #リンク(aタグのhref属性) 29 30 # 「次へ」は1つしかないが、あえてelementsで複数検索。空のリストであれば最終ページの意味になる。 31 if driver.find_elements_by_id('pnnext') == []: 32 i = i_max + 1 33 else: 34 # 次ページのURLはid="pnnext"のhref属性 35 next_page = driver.find_element_by_id('pnnext').get_attribute('href') 36 driver.get(next_page) # 次ページへ遷移する 37 i = i + 1 # iを更新 38 time.sleep(3) # 3秒間待機 39 return title_list, link_list # タイトルとリンクのリストを戻り値に指定 40 41title = ranking(driver) 42 43time.sleep(2) 44 45with open('title.txt', mode = 'w', encoding='utf-8') as f: 46 f.write("\n".join(title)) 47 48with open('link.txt', mode = 'w', encoding='utf-8') as f: 49 f.write("\n".join(link)) 50 51driver.quit() 52
Traceback (most recent call last): File "C:\Python\test0.py", line 58, in <module> title = ranking(driver) File "C:\Python\test0.py", line 27, in ranking title_list.append(elem.find_element_by_class_name('yt-simple-endpoint style-scope ytd-video-renderer').text) #タイトル(class="LC20lb") File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 398, in find_element_by_class_name return self.find_element(by=By.CLASS_NAME, value=name) File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 659, in find_element {"using": by, "value": value})['value'] File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute return self._parent.execute(command, params) File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".yt-simple-endpoint style-scope ytd-video-renderer"} (Session info: chrome=77.0.3865.120)

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

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

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

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

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

meg_

2019/10/11 16:35

youtubeの規約を読むと、スクレイピング禁止のようです。
Yuu412

2019/10/12 04:32

スクレイピングの勉強の一環としてYoutubeを選んだだけだったので、違うサイトでスクレイピングの勉強をしてみようと思います。ありがとうございました。
guest

回答1

0

自己解決

Youtubeの利用規約を確認します。

投稿2019/10/12 04:32

Yuu412

総合スコア37

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問