试图使用Python中的selenium浏览器服务器代理获取一堆urls的HAR文件。对于基本实现,我使用来自Browser暴民文档的示例代码。我下面的代码
from browsermobproxy import Server
import psutil
import time
server = Server(“/path/to/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har(“google”)
driver.get("http://www.google.com")
print(proxy.har) #ISSUE IN THIS LINE
server.stop()
driver.quit()我能够初始化浏览器代理,并获得selenium来打开firefox (和chrome)上的页面。当它到达“proxy.har”行时,它将抛出一个JSONDecodeError: Expecting值:第1行,列1 (char 0)
错误跟踪
JSONDecodeError Traceback (most recent call last)
<ipython-input-2-f690bb4c2c08> in <module>()
----> 1 proxy.har
~/anaconda3/lib/python3.6/site-packages/browsermobproxy/client.py in har(self)
102 r = requests.get('%s/proxy/%s/har' % (self.host, self.port))
103
--> 104 return r.json()
105
106 def new_har(self, ref=None, options=None, title=None):
~/anaconda3/lib/python3.6/site-packages/requests/models.py in json(self, **kwargs)
890 # used.
891 pass
--> 892 return complexjson.loads(self.text, **kwargs)
893
894 @property
~/anaconda3/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder
~/anaconda3/lib/python3.6/json/decoder.py in decode(self, s, _w)
337
338 """
--> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340 end = _w(s, end).end()
341 if end != len(s):
~/anaconda3/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
355 obj, end = self.scan_once(s, idx)
356 except StopIteration as err:
--> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
358 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)我试过很多事情来解决这个问题,但没能成功
不管我做什么,我都会犯同样的错误。我最初以为问题出在我的安装上。我就是这样做的:
有谁有类似的问题,他们解决了除了上面描述的以外的其他任何东西吗?
发布于 2018-10-30 01:58:52
问题是“浏览器-代理”包与Python版本不兼容。这个包是为Python2.x编写的,而您正在使用Python3.6。
发布于 2018-11-14 17:59:08
在OSX10.14上运行Python3.6时,我也遇到了同样的问题。
通过切换到Python3.7解决--在Linux和Mac上都能工作。
https://stackoverflow.com/questions/53050956
复制相似问题