zoukankan      html  css  js  c++  java
  • python 模拟浏览器访问网页 selenium+chromedriver+360浏览器

    要模拟浏览器访问网页,网上较普遍的是用selenium+chromedriver+chrome浏览器。

     一,安装selenium第三方库

    在cmd命令行串口输入pip install selenium

    二,安装webdriver

    网上主要有三类浏览器,chrome和firefox和ie,我习惯用360安全浏览器,它采用的是chrome内核。

    下载chromedriver,需要与浏览器的版本相对应。我浏览器的版本是63,对应的chromedriver版本是2.36,到下面的地址下载对应的版本。

    http://chromedriver.storage.googleapis.com/index.html

    将chromedriver.exe所在目录加入到windows的环境变量path中

    三,输入下面代码

    from selenium import webdriver
    browser = webdriver.Chrome()
    browser.get('http://www.baidu.com/')  

    倒霉,运行出错,主要提示如下:

    cannot find Chrome binary

    原因分析:大概意思就是chrome浏览器未找到,经过网上大量找资料,回头终于发现,只要将启动浏览器的启动exe文件改个名就行,即将360se.exe改为chrome.exe即可。

    这样,程序就可以驱动浏览器了。

    也可以这样: 

    from selenium import webdriver
    option=webdriver.ChromeOptions()
    option.binary_location=r'C:360se360se6Application360se.exe'
    drive=webdriver.Chrome(r'C:360se360se6Applicationchromedriver.exe',options=option)
    drive.get('http://www.lcez.com.cn')
    

     四,注:上面是普通模式启动浏览器,还有另外一种 静默模式,即不显示浏览器界面。

    option.add_argument('--headless')
    

      在这里出现了错误

    Message: session not created exception: Chrome version must be >= 62.0.3202.0

    原因未知,但换成chrome浏览器,则正常, 如下:

    option=webdriver.ChromeOptions()
    option.add_argument('--headless')
    drive=webdriver.Chrome(options=option)
    drive.get('http://lcez.com.cn')
    print(drive.title)
    drive.quit()
    

      

  • 相关阅读:
    JavaScript Date对象和函数 (一)
    Css3 文字渐变整理(一)
    Asp.net Core CacheHelper 通用缓存帮助类
    .net core中使用GB2312编码的问题
    苹果手机微信浏览器select标签选择完成之后页面不会自动回到原位
    .Net Core NOPI操作word(二) 表格操作
    .Net Core NOPI操作word(一)
    .NetCore中EFCore的使用整理(三)-关联表操作
    windos server2012安装.net core 2.2问题
    C# 最简单的使程序单进程运行的方法
  • 原文地址:https://www.cnblogs.com/blogzyq/p/11078530.html
Copyright © 2011-2022 走看看