1. 首页
  2. 主题
  3. 爬虫系列

selenium 问题:无法建立连接,因为目标计算机主动拒绝了它。

axing42 · · 4034 次点击
# 代码 ```go const ( seleniumPath = `D:/download/1/chromedriver.exe` port = 9999 ) var options []selenium.ServiceOption service, err2 := selenium.NewChromeDriverService(seleniumPath, port, options...) common.Must(err2) chromeCaps := chrome.Capabilities{ Path: "", Args: []string{ //"--headless", // 设置Chrome无头模式,在linux下运行,需要设置这个参数,否则会报错 //"--no-sandbox", "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36", // 模拟user-agent,防反爬 }, } //以上是设置浏览器参数 caps := selenium.Capabilities{"browserName": "chrome"} caps.AddChrome(chromeCaps) wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port)) common.Must(err) defer func() { common.Must(service.Stop()) common.Must(wd.Close()) common.Must(wd.Quit()) }() err3 := wd.Get("https://baidu.com") common.Must(err3) ``` ![屏幕截图 2021年03月12日 222832.jpg](https://static.studygolang.com/210312/81f0cb6e93480fc0fc1b23a65478addd.jpg)

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中