简体   繁体   English

Browsermob selenium python - JSONDecodeError

[英]Browsermob selenium python - JSONDecodeError

Trying to get HAR files for a bunch of urls using browsermob-proxy with selenium in Python.尝试在 Python 中使用带有 selenium 的 browsermob-proxy 获取一堆 url 的 HAR 文件。 For the basic implementation, I'm using the sample code from Browsermob documentation .对于基本实现,我使用Browsermob 文档中的示例代码。 My code below我的代码如下

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()

I'm able to initialize browsermob-proxy, and get selenium to open the page on firefox (and chrome).我能够初始化 browsermob-proxy,并让 selenium 在 firefox(和 chrome)上打开页面。 When it gets to the 'proxy.har' line, it throws a JSONDecodeError: Expecting value: line 1 column 1 (char 0)当它到达“proxy.har”行时,它会抛出一个 JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Error trace below下面的错误跟踪

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)

I've tried a bunch of things to resolve this, but can't get it to work我已经尝试了很多方法来解决此问题,但无法使其正常工作

  • The GET request returns 200 GET 请求返回 200
  • Tried some json functions to load or parse to utf-8 or strip invalid characters尝试了一些 json 函数来加载或解析为 utf-8 或去除无效字符
  • Tried simplejson试过 simplejson
  • Tried changing browsermob port尝试更改 browsermob 端口
  • Tried on different networks in case the block was being caused by proxy settings在不同的网络上尝试,以防阻止是由代理设置引起的

I get the same error no matter what I try.无论我尝试什么,我都会遇到同样的错误。 I initially thought the issue was with my installation.我最初认为问题出在我的安装上。 This is how I did it:我是这样做的:

  1. Installed selenium using pip3 (using python 3.6 with Jupyter/iPython)使用 pip3 安装硒(使用 python 3.6 和 Jupyter/iPython)
  2. Downloaded browsermob binary from https://bmp.lightbody.net/https://bmp.lightbody.net/下载 browsermob 二进制文件
  3. Downloaded geckodriver下载 geckodriver
  4. Started the proxy using ./browsermob-proxy within /bin, and also tried selecting the port using -port在 /bin 中使用 ./browsermob-proxy 启动代理,并尝试使用 -port 选择端口

Anyone had similar issues, which they resolved using anything other than the ones described above?任何人都遇到过类似的问题,他们使用上述问题以外的任何方法解决了这些问题?

The problem is that the "browsermob-proxy" package is not compatible with your Python version.问题是“browsermob-proxy”包与您的 Python 版本不兼容。 The package is written for Python 2.x, while you are using Python 3.6.该包是为 Python 2.x 编写的,而您使用的是 Python 3.6。

I had the very same issue when running Python 3.6 on OSX 10.14.在 OSX 10.14 上运行 Python 3.6 时,我遇到了同样的问题。

Solved by switching to Python 3.7 - works on both Linux and Mac.通过切换到 Python 3.7 解决 - 适用于 Linux 和 Mac。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM