Python3 + selenium + PhantomJS UnicodeDecodeError

时间:2017-12-07 10:56:30

标签: python-3.x selenium phantomjs

  • ENV

    PhantomJS2.1.1; Windows10;硒 - 3.8.0; python3

  • CODE

    browser = webdriver.PhantomJS() //throw a UnicodeDecodeError   
    
  • 问题

      

    UnicodeDecodeError:'utf-8'编解码器无法解码位置上的字节0xbd   2:无效的起始字节

  • 详细

      

    追踪(最近一次通话):         文件“d:/devtools/phantomjs-2.1.1-windows/bin/IndustryLeaderSpider.py”,   第20行,在           browser = webdriver.PhantomJS('D:/phantomjs.exe')         文件“D:\ Continuum \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ phantomjs \ webdriver.py”,   第58行,在 init 中           desired_capabilities = desired_capabilities)         文件“D:\ Continuum \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,   第154行,在 init 中           self.start_session(desired_capabilities,browser_profile)         文件“D:\ Continuum \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,   第243行,在start_session中           response = self.execute(Command.NEW_SESSION,参数)         文件“D:\ Continuum \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,   第309行,执行中           response = self.command_executor.execute(driver_command,params)         文件“D:\ Continuum \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ remote_connection.py”,   第460行,执行中           return self._request(command_info [0],url,body = data)         文件“D:\ Continuum \ Anaconda3 \ lib \ site-packages \ selenium \ webdriver \ remote \ remote_connection.py”,   第522行,在_request中           resp = opener.open(request,timeout = self._timeout)         文件“D:\ Continuum \ Anaconda3 \ lib \ urllib \ request.py”,第526行,处于打开状态           response = self._open(req,data)         文件“D:\ Continuum \ Anaconda3 \ lib \ urllib \ request.py”,第544行,在_open中           '_open',req)         文件“D:\ Continuum \ Anaconda3 \ lib \ urllib \ request.py”,第504行,在_call_chain中           result = func(* args)         文件“D:\ Continuum \ Anaconda3 \ lib \ urllib \ request.py”,第806行,in           meth(r,proxy,type))         在proxy_open中输入文件“D:\ Continuum \ Anaconda3 \ lib \ urllib \ request.py”,第814行           如果req.host和proxy_bypass(req.host):         在proxy_bypass中输入文件“D:\ Continuum \ Anaconda3 \ lib \ urllib \ request.py”,第2739行           return proxy_bypass_registry(host)         在proxy_bypass_registry中的文件“D:\ Continuum \ Anaconda3 \ lib \ urllib \ request.py”,第2706行           fqdn = socket.getfqdn(rawHost)         在getfqdn中输入“D:\ Continuum \ Anaconda3 \ lib \ socket.py”,第673行           主机名,别名,ipaddrs = gethostbyaddr(名称)       UnicodeDecodeError:'utf-8'编解码器无法解码位置2中的字节0xbd:无效的起始字节

有人有这个问题吗?如何解决?

1 个答案:

答案 0 :(得分:0)

您的主要错误似乎是:

File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 460, in execute return self._request(command_info[0], url, body=data) File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 522, in _request resp = opener.open(request, timeout=self._timeout) File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 526, in open response = self._open(req, data)

请尝试以下步骤:

  • 运行 CCleaner 工具以清除所有操作系统的杂务。
  • 选择 System Reboot
  • 当您启动 PhantomJS() 时,提供 phantomjs 二进制文件的绝对路径,如下所示:

    driver = webdriver.PhantomJS(executable_path=r'C:\path\to\phantomjs.exe')
    
  • 确保脚本中没有不必要的imports。示例: urllib.request

  • 执行Test