linux火狐浏览器和爬虫,Selenium爬虫自动化 Webdriver 驱动Chrome与Firefox 浏览器操作...

先下载chrome或者firefox驱动

以下地址下载地址是Chrome驱动的地址

Chrome历史版本下载:https://www.chromedownloads.net/chrome64linux-stable/

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

http://chromedriver.chromium.org/(这个地址需要国际网络环境)

firefox的驱动下载地址,自己找对应的版本

firefox历史版本下载地址:http://ftp.mozilla.org/pub/firefox/releases/

https://github.com/mozilla/geckodriver/releases

https://selenium-python.readthedocs.io/installation.html#drivers

1.下载自己浏览器对应 版本。

安装如果你的环境是conda那就将下载的驱动放到 conda安装目录下的Script目录中就行,放其他目录也可以,但是要将驱动目录加入环境变量。

2.另一种安装方法,可以不用将驱动地址放入环境变量,指定驱动路径。

driver = webdriver.Firefox(executable_path=firefox_driver_path)

driver = webdriver.Chrome(executable_path=chrome_driver_path)

一般下载驱动的时候会有提示,驱动的版本支持的浏览器版本,一定要下载对应的驱动。

然后就可以用了#这是一个最简单的示例,这个示例是默认将驱动加入了环境变量的路径了,所以不用指定驱动路径。

from selenium import webdriver

options = Options()

profile = FirefoxProfile()

profile.set_preference('general.useragent.override', "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36")

driver = webdriver.Firefox(options=options, firefox_profile=profile)

driver.get("https://www.xcwmoon.com")

#其他高级用法可以看它的文档。只是一个示例,需要的包要单独导入

Chrome示例:options = webdriver.ChromeOptions()

options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"')

browser = webdriver.Chrome(options=options)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值