2013-11-20 51 views

回答

3

點擊搜索按鈕,我覺得處理這是使用硒的webdriver的最簡單的方法。

鏈接:http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-webdriver

如果你有PIP安裝,簡單的

pip install selenium 

應該工作。我建議使用Firefox作爲瀏覽器。

您可以使用Selenium下載頁面,然後用BS4解析它。這是一個簡單的腳本,它將在表單中輸入「Foo」和「Bar」,然後單擊「搜索」按鈕。

from selenium import webdriver 
driver = webdriver.Firefox() 
driver.get("http://www.icsi.edu/Member/MembersDirectory.aspx") 
# Alternatively, link directly to the form: 
# driver.get("https://www.icsi.in/student/Members/MemberSearch.aspx?SkinSrc=%5BG%5DSkins/IcsiTheme/IcsiIn-Bare&ContainerSrc=%5BG%5DContainers/IcsiTheme/NoContainer") 

# Locate the elements. 
first = driver.find_element_by_id("dnn_ctr410_MemberSearch_txtFirstName") 
last = driver.find_element_by_id("dnn_ctr410_MemberSearch_txtLastName") 
search = driver.find_element_by_id("dnn_ctr410_MemberSearch_btnSearch") 

# Input the data and click submit. 
first.send_keys("Foo") 
last.send_keys("Bar") 
search.click() 

作爲獎勵,這裏是如何通過結果的頁面重複:

# next_page should be redeclared every time you visit a new page. 
next_page = driver.find_element_by_class_name("rgPageNext") 
next_page.click()