如何使用 Selenium 和 Python 获取网站的图像?

How can i get the image of a website using Selenium and Python?

提问人:simonedimaria 提问时间:12/29/2021 最后编辑:simonedimaria 更新时间:12/29/2021 访问量:218

问:

我正在使用 Selenium chrome-webdriver 和 python,我正在尝试下载或获取尝试登录此站点时弹出的图像的 base64 字符串:https://coinmarketcap.com/it/account/my-diamonds/

我试图检查元素并单击图像的链接,但我得到:“403 ERROR The request could not be satisfied.”,可能我没有此图像的权限。

我还尝试了以下代码:

ele = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '/html/body/div[4]/div/div[2]/div[1]')))

# get the img as a base64 string
img_base64 = driver.execute_async_script("""
    var ele = arguments[0], callback = arguments[1];
    ele.addEventListener('load', function fn(){
      ele.removeEventListener('load', fn, false);
      var cnv = document.createElement('canvas');
      cnv.width = this.width; cnv.height = this.height;
      cnv.getContext('2d').drawImage(this, 0, 0);
      callback(cnv.toDataURL('image/jpeg').substring(22));
    }, false);
    ele.dispatchEvent(new Event('load'));
    """, ele)

print(img_base64)

# save the img to a file
with open(r"img.jpg", 'wb') as f:
    f.write(base64.b64decode(img_base64))

但我得到:

selenium.common.exceptions.TimeoutException:消息:脚本超时

那么,我怎样才能以某种方式做到这一点呢?

python selenium 网页抓取 selenium-chromedriver chrome-web-driver

评论

0赞 Sparrow1029 12/29/2021
所以你不能得到这个链接?另外,它是一个 SVG 图像 - 为什么要将其保存到 base64?
0赞 simonedimaria 12/29/2021
@Sparrow1029对不起,我指的是这个链接:bin.bnbstatic.com/image/antibot/image/SLIDE/20211228/11/... (我在问题中犯了一个错误)
0赞 edd 12/29/2021
内置方法 - docs
0赞 simonedimaria 12/29/2021
@edd但是,我怎样才能只对元素进行屏幕截图,而不是对整个页面进行截图呢?

答: 暂无答案