香港の法律の内容を撤回したいと思います。しかし、ページをスクロールしない限り、表示されていないコンテンツにアクセスするのに問題があります。香港の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のコンテンツウィンドウのスクロールバーとその下にスクロールします。
私はあなたがコードは(oythonバージョンに依存する)ものの、依然としてエラーになります疑い。私はpy3 .textはすでにutf-8でエンコードされているので、文字列はデフォルトごとにunicodeです。 – jlaur
あなたの最初の解決策は私のためには機能しませんでした。しかし、ソリューション2を提案してくれてありがとう。あなたの提案したソリューションを少し修正した後、私はそのコンテンツにアクセスできた。 –
申し訳ありませんが、 ";" JavaScriptの終わりに。私はコードを実行しませんでした。今は動作しますが、あなたの後のコンテンツがフレーム内にあるため、特定のサイトでは動作しません。だから、あなたがフレームを入力してその中をスクロールするのを助けたいのであれば、それについて質問してください。 – jlaur