私はYoutube Gamingでライブチャンネル/視聴者のリストを収集しようとしています。私はPythonでセレンを使ってウェブサイトにページをスクロールさせ、その11個以上のチャンネルを読み込ませるようにしています。参考までに、私が取り組んでいるウェブページはthisです。Selenium with Pythonで問題を発見する
私が望むデータの場所が見つかりましたが、セレンがそこに行くのを苦労しています。一部は、私はこのようなルックスとのトラブルを抱えています:現在
<div class="style-scope ytg-gaming-video-renderer" id="video-metadata"><span class="title ellipsis-2 style-scope ytg-gaming-video-renderer"><ytg-nav-endpoint class="style-scope ytg-gaming-video-renderer x-scope ytg-nav-endpoint-2"><a href="/watch?v=FFKSD1HHrdA" tabindex="0" class="style-scope ytg-nav-endpoint" target="_blank">
Live met Bo3
</a></ytg-nav-endpoint></span>
<div class="channel-info small layout horizontal center style-scope ytg-gaming-video-renderer">
<ytg-owner-badges class="style-scope ytg-gaming-video-renderer x-scope ytg-owner-badges-0">
<template class="style-scope ytg-owner-badges" is="dom-repeat"></template>
</ytg-owner-badges>
<ytg-formatted-string class="style-scope ytg-gaming-video-renderer">
<ytg-nav-endpoint class="style-scope ytg-formatted-string x-scope ytg-nav-endpoint-2"><a href="/channel/UCD8Q9V5wgo8o0XGfUqsRrDQ" tabindex="0" class="style-scope ytg-nav-endpoint" target="_blank">Rico Eeman</a>
</ytg-nav-endpoint>
</ytg-formatted-string>
</div><span class="ellipsis-1 small style-scope ytg-gaming-video-renderer" id="video-viewership-info" hidden=""></span>
<div id="metadata-badges" class="small style-scope ytg-gaming-video-renderer">
<ytg-live-badge-renderer class="style-scope ytg-gaming-video-renderer x-scope ytg-live-badge-renderer-1">
<template class="style-scope ytg-live-badge-renderer" is="dom-if"></template>
<span aria-label="" class="text layout horizontal center style-scope ytg-live-badge-renderer">4 watching</span>
<template class="style-scope ytg-live-badge-renderer" is="dom-if"></template>
</ytg-live-badge-renderer>
</div>
</div>
、私がしようとしています。しかし
#This part works fine. I can use the unique ID
meta_data = driver.find_element_by_id('video-metadata')
#This part is also fine. Once again, it has an ID.
viewers = meta_data.find_element_by_id('metadata-badges')
print(viewers.text)
、私はこの例'Rico Eeman'
では(トラブルチャンネル名を取得を抱えているのです、最初のネストされたdivタグの下にあります)。その化合物のクラス名なので、私はクラス名で要素を見つけることができない、としようとして次のXPathは仕事doesntの:彼らの両方がエラーが見つかりません要素を高める
name = meta_data.find_element_by_xpath('/div[@class="channel-info small layout horizontal center style-scope ytg-gaming-video-renderer"]/ytg-formatted-string'
name = meta_data.find_element_by_xpath('/div[1])
を。私は本当にここで何をすべきかはわかりません。誰かが実用的な解決策を持っていますか?
ありがとう、CSSセレクターが完璧に機能しました!しかし、result.textでxpathの結果を出力すると、空文字列が出力されます。 編集:cssセレクタで動作するので、どちらの問題も問題ありません! :) – Pieter