提问人:simonedimaria 提问时间:12/29/2021 最后编辑:simonedimaria 更新时间:12/29/2021 访问量:218
如何使用 Selenium 和 Python 获取网站的图像?
How can i get the image of a website using Selenium and Python?
问:
我正在使用 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:消息:脚本超时
那么,我怎样才能以某种方式做到这一点呢?
答: 暂无答案
评论