2017-07-03 23 views
0

香港の法律の内容を撤回したいと思います。しかし、ページをスクロールしない限り、表示されていないコンテンツにアクセスするのに問題があります。香港のe-法律をウェブで掻き集める

私がアクセスしていますウェブサイト:How can I scroll a web page using selenium webdriver in python?から取った次のコードは、ブラウザをスクロールするために使用されていることを理解https://www.elegislation.gov.hk/hk/cap211

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException 
from selenium.common.exceptions import ElementNotVisibleException 
from selenium.webdriver.common.action_chains import ActionChains 

def init_driver(profile): 
    driver = webdriver.Firefox(profile) 
    driver.wait = WebDriverWait(driver, 5) 
    return driver 

def convert2text2(webElement): 
    if webElement != []: 
     webElements = [] 
     for element in webElement: 
      e = element.text.encode('utf8') 
      webElements.append(e) 
    else: 
     webElements = ['NA'] 
    return webElements 

profile = webdriver.FirefoxProfile() 
driver = init_driver(profile) 
url = 'https://www.elegislation.gov.hk/hk/cap211' 
driver.get(url) 
driver.wait = WebDriverWait(driver, 5) 

content = driver.find_elements_by_xpath("//div[@class='hklm_content' or @class='hklm_leadIn' or @class='hklm_continued']") 
content = convert2text2(content) 

を:

SCROLL_PAUSE_TIME = 0.5 

# Get scroll height 
last_height = driver.execute_script("return document.body.scrollHeight") 

while True: 
    # Scroll down to bottom 
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 

    # Wait to load page 
    time.sleep(SCROLL_PAUSE_TIME) 

    # Calculate new scroll height and compare with last scroll height 
    new_height = driver.execute_script("return document.body.scrollHeight") 
    if new_height == last_height: 
     break 
    last_height = new_height 

しかし、私が指定する方法を見つけ出すcouldntのコンテンツウィンドウのスクロールバーとその下にスクロールします。

答えて

1

あなたはそうのようなJavaScriptコードにlast_height入れる:

while True: 
    # Scroll down to 'last_height' 
    driver.execute_script("window.scrollTo(0, {});".format(last_height)) 

    # Wait to load page 
    time.sleep(SCROLL_PAUSE_TIME) 

    # Calculate new scroll height and compare with last scroll height 
    new_height = driver.execute_script("return document.body.scrollHeight;") 
    if new_height == last_height: 
     break 
    last_height = new_height 

単にセレンことなくデータを引っ張っだろうこのついて行くの別の方法を。ページメイク(Chromeインスペクタ、[ネットワーク]タブ)の呼び出しを見ると、新しい要素がそれぞれxmlの小さなチャンクを使用してサイトに読み込まれることがわかります。出発点のため

URLは「https://www.elegislation.gov.hk/xml?skipHSC=true&LANGUAGE=E&BILINGUAL=&LEG_PROV_MASTER_ID=181740&QUERY=.&INDEX_CS=N&PUBLISHED=true

PROV_MASTER_IDパラメータは、各チャンクサイトの負荷のために1ずつ増加しますです。

あなたはそうのような要求を使用して、すべてをつかむことができます:

import requests 
url = 'https://www.elegislation.gov.hk/xml?skipHSC=true&LANGUAGE=E&BILINGUAL=&LEG_PROV_MASTER_ID={}&QUERY=.&INDEX_CS=N&PUBLISHED=true' 
starting_count = 181740 
stop_count = "" # integer - you need to figure out, when you got all you need 
count = starting_count 
while count <= stop_count: 
    response = requests.get(url.format(count)) 
    # parse the xml and grab the parts you need... 
    count +=1 
+0

私はあなたがコードは(oythonバージョンに依存する)ものの、依然としてエラーになります疑い。私はpy3 .textはすでにutf-8でエンコードされているので、文字列はデフォルトごとにunicodeです。 – jlaur

+0

あなたの最初の解決策は私のためには機能しませんでした。しかし、ソリューション2を提案してくれてありがとう。あなたの提案したソリューションを少し修正した後、私はそのコンテンツにアクセスできた。 –

+0

申し訳ありませんが、 ";" JavaScriptの終わりに。私はコードを実行しませんでした。今は動作しますが、あなたの後のコンテンツがフレーム内にあるため、特定のサイトでは動作しません。だから、あなたがフレームを入力してその中をスクロールするのを助けたいのであれば、それについて質問してください。 – jlaur

関連する問題