2016-10-13 2 views
1

セレンを使用すると、スクリプトに特定の条件を満たす要素を選択させる方法がありますか?セレニウム(Python)を使用して基準を満たすすべての要素を選択

私が正確にしようとしているのは、セレンがX人以上の視聴者を持つすべてのツイッチチャンネルを選択していることです。あなたは要素を調べる場合は、この見つける:

<p class="info" 
    562 
    viewers on 
    <a class="js-profile-link" href="/hey_jase/profile" 
    data-tt_content="live_channel" data-tt_content_index="1" 
    data-tt_medium="twitch_directory" data-ember-action="1471"> 
    Hey_Jase 
    </a> 
</p> 

答えて

0

を私たちはHTMLのみの小さなサンプルを得るように、正確なコードを与えることは難しいですが、あなたはそれを少し微調整する場合は、この作業をする必要があります。

from selenium import webdriver 
driver = webdriver.Firefox() 
driver.get('http://www.website.com') 

source = driver.page_source 
location = source.find('<p class="info"') 
source = source[location+16:] #adjust the +16 to maybe +15 or w/e depending on the exact source page you get 
location_second = source.find('viewers on') #assuming there is a space between the actual number of viewers and the 
source = int(source[:location_second-1]) #adjust the -1 to maybe -2 or w/e depending on the exact source page you get 
if source > x: # replace x with whatever number is your minimum viewers 
    driver.find_element_by_class_name('js-profile-link') #might need to use x-path if you have multiple instances of the same class name 
+0

ソース[場所+ 16]が正確に何をしますか? 。 – Astrum

+0

source = source [location + 16:] - > locationは、 "

willer2k

1

まず、すべての攣縮チャンネルのリンクを見つけることができます。次に、ビュー数に基づいてフィルタリングします。これらの線に沿って

何か:

import re 
from selenium import webdriver 


THRESHOLD = 100 

driver = webdriver.Firefox() 
driver.get("url") 

pattern = re.compile(r"(\d+)\s+viewers on") 
for link in driver.find_elements_by_css_selector("p.info a[data-tt_content=live_channel]"): 
    text = link.find_element_by_xpath("..").text # get to the p parent element 
    match = pattern.search(text) # extract viewers count 
    if match: 
     viewers_count = int(match.group(1)) 
     if viewers_count >= THRESHOLD: 
      print(link.text, viewers_count) 
+0

何をインポートしていますか、パターンのパラメータはどこから来たのですか? – Astrum

+0

@Astrum私は少しスニペットを少し明瞭にしたいと思っています。パターンは、p要素のテキストから視聴者の数を抽出することです。 – alecxe

+0

私は本当に迷っています。私は私の質問をリストアップすると思います。 1. re.compileとは何ですか、そしてそのパラメータはどこから来ますか。 2.どこでそのCSSパスを取得しましたか?私がファイヤーバグを使用してビューア要素へのパスを見つけると、非常に長い文字列が得られます。 3.テキスト=とは何ですか?やっている?私はcss_selectorメソッドが私たちの要素を見つけると思った。 – Astrum

関連する問題