1. 程式人生 > >關於反爬蟲,我用實際案例告訴你(三大方法)

關於反爬蟲,我用實際案例告訴你(三大方法)

from selenium import webdriverimport time#建立一個瀏覽器客戶端,並且指定配置#如果是PhantomJS做瀏覽器客戶端還要設定路徑driver = webdriver.PhantomJS(executable_path="/usr/local/bin/phantomjs")# driver = webdriver.Chrome()   表示用谷歌瀏覽器driver.get("https://www.douban.com/")time.sleep(1)driver.save_screenshot("豆瓣首頁.png")#輸入賬號driver.find_element_by_id(

"form_email").send_keys("賬號")#輸入密碼driver.find_element_by_name("form_password").send_keys("密碼")#儲存驗證碼的圖片driver.save_screenshot("驗證碼.png")#輸入驗證碼check_code = input("請輸入驗證碼:")print(r"驗證碼是多少:%s" % check_code)driver.find_element_by_id("captcha_field").send_keys(check_code) #點選登入按鈕driver.find_element_by_xpath(
"//input[@class='bn-submit']").click()#休眠一下等待登入成功time.sleep(3)#儲存登入成功的快照driver.save_screenshot("登入成功.png")#儲存成功登入好的html到本地with open("douban.html","w",encoding="utf-8") as f:   f.write(driver.page_source)#退出成功driver.quit()