4
我剛剛安裝了Ghost.py爲了刮一些網站,需要我有JavaScript。無論如何,在當前頁面上獲得可重複使用的表單列表,就像機械化模塊將使用mechanize.Browser().forms()
一樣?或者,如果沒有,我可以通過頁面(所有JavaScript的東西已經加載)到機械庫,並讓它填寫/提交表單?Ghost.py返回列表形式
我剛剛安裝了Ghost.py爲了刮一些網站,需要我有JavaScript。無論如何,在當前頁面上獲得可重複使用的表單列表,就像機械化模塊將使用mechanize.Browser().forms()
一樣?或者,如果沒有,我可以通過頁面(所有JavaScript的東西已經加載)到機械庫,並讓它填寫/提交表單?Ghost.py返回列表形式
硒可以爲你做到這一點,如果你不介意瀏覽器彈出在你的屏幕上。它也可以無頭奔跑,但這很棘手。一個簡單的辦法:
from selenium import webdriver
driver = webdriver.Firefox()
url = "http://www.w3schools.com/html/html_forms.asp"
driver.get(url)
# get a list of the page's forms as Selenium WebElements
# (webdriver API ref: http://selenium-python.readthedocs.org/en/latest/api.html)
forms = driver.find_elements_by_xpath(".//form")
for i, form in enumerate(forms):
print i, form.text
# the last form, index number 5, has input tags of type "text" and "submit"
"""
<form name="input0" target="_blank" action="html_form_action.asp" method="get">
"
Username: "
<input type="text" name="user" size="20">
<input type="submit" value="Submit">
</form>
"""
# get the input WebElements from this form WebElement
inputs = forms[5].find_elements_by_xpath(".//input")
# write text to the text input, then submit the form
inputs[0].send_keys('hihi frds!')
inputs[1].submit()
好帖子...但是我可以不使用PhantomJS驅動程序而不是Firefox webdriver,通過只更改1行(driver = webdriver.PhantomJS)來完成這個任務。 –
http://stackoverflow.com/questions/15513699/how-can-i-extract-the-list-of-urls-obtained-during-a-html-page-render- in-python – 2013-05-27 07:31:32