問 題
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"]=("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
url ="http://wenshu.court.gov.cn/User/RegisterAndLogin?Operate=1"
driver = webdriver.PhantomJS(executable_path='C:\\Users\\wang\\Downloads\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
driver.get(url)
import time
time.sleep(30)
driver.find_element_by_xpath('//*[@id="login_Name"]').send_keys("398915607@qq.com")
driver.find_element_by_xpath('//*[@id="login_Pwd_Temp"]').send_keys("wstktb")
driver.find_element_by_xpath('//*[@id="btnLogin"]').click()
driver.quit()
chrome上觀察源碼明明都有的,這個網站看來看去也沒有frame的標簽,打印page_source也只有幾行js代碼。,小弟最近剛開始學習爬蟲,實在是十分困惑啊,懇請各位求教。
解決方案
我測試了一下,第一次打開這個頁面的時候,如果你沒有他需要的cookie,那么你進入的就是那個全是js的頁面,HTTP狀態碼202,此時該域名下會生成一個cookie(我不確定怎么來的)。
然后瀏覽器會自動刷新,js會在請求中加入一段cookie(該cookie不保存在瀏覽器,且每次都不一樣),服務端返回正常的頁面,HTTP狀態碼200。
題主可以用chrome的view-source來測試。
所以你說的source只有幾行代碼那就是第一次打開的情況,我對selenium不是很了解,不過應該是因為沒有自動跳轉的問題,具體原因可能是 你的配置、js中有判斷。
掃一掃關注IT屋
微信公眾號搜索 “ IT屋 ” ,選擇關注與百萬開發者在一起