【发布时间】:2021-03-24 10:40:43
【问题描述】:
我正在编写一个 Python Selenium 刮板,它应该从网页下载所有图像。该网页要求您将鼠标悬停在元素上以显示我已经自己解决的图像。有(APNG - 动画 png)PNG 图像,刮板下载得很好,因为它会拉 src 标签。当刮板到达一个包含在 SVG 标签中但没有源链接的动画 SVG 时,就会发生致命问题。在我看来,图像直接嵌入在代码本身中。我尝试寻找解决方案,但到目前为止我没有发现任何东西可以帮助我实现自动化。每个人都只是参考 svg-grabber Chrome 插件,这似乎是一种解决方法而不是解决方案,而且也不能自动化。
这是我遇到问题的页面:
(您需要将鼠标悬停在使用标准 APNG 的“直接限制”列中的项目上)
如何将此图像下载为 SVG?
这是我处理图片下载的一段代码:
hover = ActionChains(driver).move_to_element(item).perform()
img = wait.until(ec.presence_of_element_located((By.XPATH, "//img[@id='img']"))).get_attribute('src')
print(img)
img_request = requests.get(img)
if img_request.ok:
with open(item.text + ".apng.png", 'wb') as imgfile:
print(f"Downloading {item.text}")
imgfile.write(img_request.content)
else:
print("Request failed.")
input("Press any key to exit")
exit()
有没有类似的下载嵌入式 SVG 的方法?
【问题讨论】:
标签: python python-3.x selenium selenium-webdriver geckodriver