Python使用Selenium控制Chrome浏览器(web自动化)

环境windows10  anaconda python2.7 selenium 

 

自己Chrome浏览器和chromedriver版本必须保持一致

(自己下载https://chromedriver.storage.googleapis.com/index.html)

我使用的是最新版8.0.41417

1、将下载后的webdriver包解压到文件夹,将该文件夹添加到系统环境变量

2、使用python导入selenium

#coding=utf8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
option=Options()
#1.创建Chrome浏览器对象,这会在电脑上在打开一个浏览器窗口
browser = webdriver.Chrome(chrome_options=option,executable_path="D:\webriver\chromedriver.exe")

#通过浏览器向服务器发送URL请求
browser.get("http://baidu.com")

(若要爬取页面啥的被webdriver这个东西检测到了,加入下面这个)

#去除webdriver标记不被检测到
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})

其他操作太多不一个个写出来了,百度,按需取用

效果如图

image.png
posted @ 2020-07-19 00:36  HumorChen99  阅读(9)  评论(0编辑  收藏  举报  来源