2016-04-25 9 views
0

私は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]) 

を。私は本当にここで何をすべきかはわかりません。誰かが実用的な解決策を持っていますか?


答えて

1

<ytg-formatted-string>タグに含まれていない名前の1つの子孫です。あなたのXPathはすべての名前を取得しないでしょうvideo-metadataを使用して働いていた場合でも、IDはあなたので、各ユーザーのためのdivごとに繰り返され、

meta_data.find_element_by_css_selector('.style-scope.ytg-formatted-string.x-scope.ytg-nav-endpoint-2 > a') 

それともxpath

meta_data.find_element_by_xpath('//ytg-nav-endpoint[@class="style-scope ytg-formatted-string x-scope ytg-nav-endpoint-2"]/a') 
+0

ありがとう、CSSセレクターが完璧に機能しました!しかし、result.textでxpathの結果を出力すると、空文字列が出力されます。 編集:cssセレクタで動作するので、どちらの問題も問題ありません! :) – Pieter

0

では、このすべての名前を取得します試してみてくださいfind_elementsが必要になりますし、返された要素を反復する:

あなたを与える
names = dr.find_elements_by_css_selector("a.style-scope.ytg-nav-endpoint[href^='/channel/']") 
print([name.get_attribute("text") for name in names]) 

['NinjaNation Gaming', 'DURX DANIEL', 'DEMON', 'Perfection', 'The one and only jd', 'Violator Games', 'KingLuii718', 'NinjaNation Gaming', 'DURX DANIEL', 'DEMON', 'Perfection'] 
+0

find_elements_by_css_selector() – Pieter

+0

@Pieterの最後に何かが見つからないことがあります。うん、固定。 –

関連する問題