Python爬蟲(二十二)_selenium案例:模擬登陸豆瓣

本篇博客主要用于介紹如何使用selenium+phantomJS模擬登陸豆瓣,沒有考慮驗(yàn)證碼的問題,更多內(nèi)容,請(qǐng)參考:Python學(xué)習(xí)指南

#-*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

import time

#如果獲取頁面時(shí)獲取不到文本內(nèi)容,加入下面參數(shù)
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
driver.set_window_size(1366, 768)
driver.get("http://www.douban.com/")

print(driver.page_source)
#輸入賬號(hào)和密碼

driver.find_element_by_name("form_email").send_keys("xxxxx@qq.com")
driver.find_element_by_name("form_password").send_keys('xxxx')

#模擬點(diǎn)擊登錄
driver.find_element_by_xpath("http://input[@class='bn-submit']").click()

#等待3秒
time.sleep(3)

#生成登錄后快照

with open('douban.html', 'w') as file:
    file.write(driver.page_source.encode('UTF-8'))

driver.quit()

期初使用driver = webdriver.PhantomJS(),返回的只是<html><head></head><body></body></html>,增加了參數(shù)就可以了

參考:

  1. 獲取不到內(nèi)容,問題參考
  2. Selenium與phantomJS 登入豆瓣 有bug
  3. Python實(shí)例:通過selenium模擬登陸豆瓣
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容