我试图用谷歌搜索并在 stackOverflow 上搜索类似的问题,但仍然无法解决我的问题。
我需要我的 python 脚本来通过代理执行 http 连接。
下面是我的测试脚本:
import urllib2, urllib
proxy = urllib2.ProxyHandler({'http': 'http://255.255.255.255:3128'})
opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://www.whatismyip.com/')
return_str = conn.read()
webpage = open('webpage.html', 'w')
webpage.write(return_str)
webpage.close()
Run Code Online (Sandbox Code Playgroud)
这个脚本在我的本地计算机(Windows 7,Python 2.7.3)上运行得非常好,但是当我尝试在服务器上运行它时,它给了我以下错误:
Traceback (most recent call last):
File "proxy_auth.py", line 18, in <module>
conn = urllib2.urlopen('http://www.whatismyip.com/')
File "/home/myusername/python/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/home/myusername/python/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/home/myusername/python/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/home/myusername/python/lib/python2.7/urllib2.py", …Run Code Online (Sandbox Code Playgroud) 我的代码:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
opts = Options()
opts.add_argument("user-data-dir=/path/to/profiles_dir/user_id")
browser = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=opts)
Run Code Online (Sandbox Code Playgroud)
当我为 id = 1 的用户启动 Chromium 时,它开始正常并创建一个配置文件目录 /path/to/profiles_dir/1。然后我访问某个任意站点并关闭浏览器。当我第二次执行上面的代码时,它抛出异常。
selenium.common.exceptions.WebDriverException:消息:未知错误:无法解析内部 JSON 模板:行:1,列:1,意外标记。(驱动信息:chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.4.0-112-generic x86_64)
Chromium 64.0.3282.119 基于 Ubuntu 构建,在 Ubuntu 16.04 上运行
ChromeDriver 2.35
硒 3.8.1
我用谷歌搜索了很多,但找不到这个问题的解决方案。为什么我不能使用现有的用户配置文件目录加载浏览器?我究竟做错了什么?