【问题标题】:webDriver.Chrome() can't get url with query stringwebDriver.Chrome() 无法获取带有查询字符串的 url
【发布时间】:2017-02-28 15:09:18
【问题描述】:

我有使用查询字符串url下载图片的代码,代码如下,我该怎么做才能解决这个问题?

urlImage = "http://ipcamera-viewer.com/image/?p=199619_20170221_162149_7208.jpg"
driver = webdriver.Chrome()
driver.set_window_position(-10000, 0)
driver.get(urlImage)                           <- Error Here
imgBase64 = driver.get_screenshot_as_base64()
imgBytes = BytesIO(base64.b64decode(imgBase64))
imgBytes.seek(0)
imgData = PIL.Image.open(imgBytes)
if rotationAngle != 0: imgData = imgData.rotate(rotationAngle, expand=True)
imgData.save(pathImage)

例外:

self.run()
self._target(*self._args, **self._kwargs)
driver.get(urlImage)
self.execute(Command.GET, {'url': url})
response = self.command_executor.execute(driver_command, params)
return self._request(command_info[0], url, body=data)
resp = self._conn.getresponse()
raise ResponseNotReady(self.__state)
http.client.ResponseNotReady: Idle

【问题讨论】:

  • 我运行了您的代码,它运行良好。我使用的是 2.53.5 selenium 和 56.0 版本的 chrome?你能提到你的铬和硒版本吗?另外,你能打开其他网站吗?
  • Mac和Windows我都试过了,Windows版本是3.0.256.0.2924.87,Mac selenium也在3.0以上,如果真的想知道的话我今晚可以告诉你..跨度>
  • 你可以尝试降级 Firefox 版本。不确定,但如果这是由于兼容性问题,它可能会解决。因为 Selenium 2 一切正常。
  • 我尝试了 Firefox,但似乎无法抓取 JavaScript,对吗?
  • 请不要在得到答案后更改问题中的代码,因为这会使答案变得毫无意义。如果您有后续问题,请将它们作为新问题发布。您可能还想看看tourHow to Ask

标签: python image selenium get selenium-chromedriver


【解决方案1】:

requests代替selenium怎么样?

如果你有特定的URL来获取资源,你可以使用requests.get

确保你已经用 pip 安装了requests

代码如下:

import requests
import shutil

url = 'http://ipcamera-viewer.com/image/?p=199619_20170221_162149_7208.jpg'
resp = requests.get(url, stream=True)
with open('199619_20170221_162149_7208.jpg', 'wb') as file:
    shutil.copyfileobj(resp.raw, file)

【讨论】:

  • 我修改了上面的代码,如果使用你的代码,如何旋转图像?
  • 查看此链接 :)
  • 对了,shutil.copyfileobj 会把图片保存到本地,我想先旋转再保存...
猜你喜欢
  • 2020-04-06
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
相关资源
最近更新 更多