今天在mac环境下尝试了一下用selenium连接现有的服务器,本来想绕过某宝的反爬虫机制的,但是并没有什么用,但是这个技术不错,我这里分享一下实现过程。
export PATH="/Applications/Google Chrome.app/Contents/MacOS:$PATH"
把上面的这一句添加到bashrc中,我的是zshrc,然后激活环境:
source ~/.zshrc
然后打开chrome:
Google\ Chrome --remote-debugging-port=9222 --user-data-dir="~/ChromeProfile"
运行这个后,就可以看见一个chrome打开了,接下来写程序连接它:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
# options.add_experimental_option('excludeSwitches', ['enable-automation'])
browser = webdriver.Chrome(executable_path=chromedriver_path, options=options)
url='https://www.tmall.com/'
browser.get(url)
运行上面的代码,会发现它连接到的是你刚才打开的浏览器,是不是很简单。
[1].How to connect Selenium to an existing browser that was opened manually?.
https://cosmocode.io/how-to-connect-selenium-to-an-existing-browser-that-was-opened-manually/
今天在mac环境下尝试了一下用selenium连接现有的服务器,本来想绕过某宝的反爬虫机制的,但是并没有什么用,但是这个技术不错,我这里分享一下实现过程。添加环境变量export PATH="/Applications/Google Chrome.app/Contents/MacOS:$PATH"把上面的这一句添加到bashrc中,我的是zshrc,然后激活环境:source ~/.zshrc然后打开chrome:Google\ Chrome --remote-debugging
# 加载谷歌浏览器驱动
path = "/Users/juanmao/PycharmProjects/chromedriver.exe"
driver = webdriver.Chrome(path)
#
打开
百度搜索网址首页
driver.get('http://www.baidu.com')
print(d.
source ~/.zshrc 或者重新
打开
terminal
Google\ Chrome --remote-debugging-port=9222 --user-data-dir="~/ChromeProfile"
将
打开
一个新的浏览器,这在里登陆啊啥的
正常
selenium
的代
在
Selenium
中使用不同的Webdriver可能会有不一样的方法,有些相同的操作会得到不一样的结果,本文主要介绍的是Chrome()的使用方法。
其他Webdriver可以查阅官方文档。
Chrome Options
这是一个Chrome的参数对象,在此对象中使用add_argument()方法可以添加启动参数,添加完毕后可以在初始化Webdriver对象时将此Options对象传入,则可...
最近做在一些 web 自动化(其实是用 web 端来配置网络设备)
编写脚本时经常用到 debug 去提取网页中的一些元素并做测试,但是每次需要 debug 时都要
打开
新的浏览器,比较麻烦,所以如果能直接
连接
到
已经
打开
的浏览器,会非常方便。
操作步骤:
使用参数启动浏览器
cmd 定位到 chrome 所在目录,使用以下参数启动:
C:\Program Files (x86)\Google\C...
使用
selenium
接管
已经
打开
的
Chrome浏览器
,并以百度网站为例,输出“百度一下”标签元素。
注:1. 首先需要用cmd命令启动浏览器,然后手动登陆所需要访问的网址,分别输入以下命令:
其次 用命令
打开
浏览器
2.输入上面命令后会显示如下
3. 再次在
打开
的浏览器中输入网址
(比如 https://www.baidu.com/)
4. 然后新建一个文件夹,将与您终端版本匹配的chromedriver驱动器放在文件夹中,如下
注:若不知chromedriver下载地址的,请参考.........
在
Mac
电脑用python+
selenium
打开
Chrome还遇到几个问题,这里总结下,以防忘记,也可帮到别人。
前提:已安装好python3,
selenium
安装chromedriver
1.先查看Chrome的版本,我这里是87
2. 下载与
Chrome浏览器
对应的chromedriver版本,下载
链接
:
https://sites.google.com/a/chromium.org/chromedriver/downloads
3. 把下载的文件解压,把chromedriver.exe拷到/usr
保存后退出,然后启动一个窗口:
Google\ Chrome --remote-debugging-port=19222 --user-data-dir="~/ChromeProfile"
其中Google\ Chrome为一个命令,把chrome添加到环境变量就可以看
win7 环境,主要是一开始想在代码中先用exec.Command启动chrome,但始终不能成功监听9222端口,折腾了很长时间,
需要先手工启动chrome监听端口(具体写在代码注释中了)然后再运行代码,在开源代码基础上稍作修改,将访问不了的google换成sohu。
效果就是通过代码将浏览器导航到了sohu.com
// Command standalone is a chrome...
2022年几款前沿的文本语义检索/Sentence Embedding方法:Gradient Cache, SGPT,ART,DPTDR,RocketQAv2, ERNIE-Search等
农民小飞侠:
2022年几款前沿的文本语义检索/Sentence Embedding方法:Gradient Cache, SGPT,ART,DPTDR,RocketQAv2, ERNIE-Search等
20222926徐少飞:
spark 2.2:jupyter notebook NameError: name 'sc' is not defined
糖醋排骨十年专家厨师:
No matching distribution found for setuptools_scm
holycun:
【PaddlePaddle】动手实现Focal Loss
101lucky: