使用Selenium與PhantomJS模擬登陸豆瓣:https://www.douban.com/html
#!/usr/bin/python3 # -*- coding:utf-8 -*- __author__ = 'mayi' """ 模擬登陸豆瓣:https://www.douban.com/ """ from selenium import webdriver # 調用環境變量指定的PhantomJS瀏覽器建立瀏覽器對象,executable_path:指定PhantomJS位置 driver = webdriver.PhantomJS(executable_path = r"D:\Program Files\phantomjs\bin\phantomjs") # get()方法會一直等到頁面被徹底加載,而後纔會繼續程序 driver.get("https://www.douban.com/") # 等待3秒 driver.implicitly_wait(3) # 經過電腦登錄前的快照 driver.save_screenshot("DoubanHome.jpg") email = input("請輸入您的郵箱/手機:") password = input("請輸入您的密碼:") # 輸入帳號密碼: driver.find_element_by_id("form_email").send_keys(email) driver.find_element_by_id("form_password").send_keys(password) # 模擬點擊登陸 driver.find_element_by_xpath("//input[@class='bn-submit']").click() # 等待3秒 driver.implicitly_wait(3) # 生成登錄後的快照 driver.save_screenshot("DoubanLanding.jpg") # 保存登錄後的源代碼 with open("douban.html", "w", encoding = "utf-8") as f: f.write(driver.page_source) # 關閉當前頁面,若是隻有一個頁面,會關閉瀏覽器 driver.close() # 關閉瀏覽器 driver.quit()