使用硒下载图像的正确方法

时间:2019-05-11 01:15:29

标签: selenium

我读过一些文章,提到如何通过selenium获取图像。例如:

    from selenium import webdriver
    import requests
    driver=webdriver.Firefox()
    driver.get("http/https://your website")
    img=driver.find_element_by_xpath("xpath leading to your element")#locating element
    src=img.get_attribute('src')#fetch the location of image
    img=requests.get(src)#fetch image
    with open('image.jpg','wb') as writer:#open for writing in binary mode
        writer.write(img.content)#write the image

但是这种方法是否存在带宽成本更高的风险?

有什么办法就像我右键单击图像并将其save as连到本地PC一样吗?

我尝试使用javascript来做到这一点:

var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var img = document.getElementById('someImageId');
context.drawImage(img, 0, 0 );
var theData = context.getImageData(0, 0, img.width, img.height);

并遇到cross-origin问题

Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
    at <anonymous>:5:23

解决方法是再次发出请求,就像我在第一行中不需要的一样。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

为了避免增加网络占用空间,可以考虑采用以下方法:

  1. 使用get_screenshot_as_png function
  2. 来获取整个页面的屏幕截图
  3. 获取必需的元素locationsize
  4. 通过剪切除所需元素坐标以外的任何内容,提取页面的“有趣”部分
  5. 保存生成的文件

将徽标从https://experitest.com/网站存储到logo.png文件的示例代码:

from selenium import webdriver
from PIL import Image
from io import BytesIO

options = webdriver.ChromeOptions()
options.add_argument("--kiosk")

driver = webdriver.Chrome(chrome_options=options)
driver.get('chrome://settings/')
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.0);')
driver.get("https://experitest.com/")
element = driver.find_element_by_xpath("//a[@class='navbar-brand']/img")
location = element.location
size = element.size
png = driver.get_screenshot_as_png()
im = Image.open(BytesIO(png))
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom))
im.save('logo.png')
driver.quit()

假设:

  • 您已安装Pillow库(应该像pip install pillow命令一样简单)
  • 您的操作系统DPI scale level设置为100%