Selenium:如何从网站中提取所有图像

我需要使用Selenium从网站中提取所有图像。这应该包括来自 html、css 和 javascript 的任何扩展名(pngjpgsvg等)的所有图像。这意味着简单地提取所有<img>元素是不够的(例如,将丢失任何从 css 样式加载的图像):

images = driver.find_elements_by_tag_name('img')  # not sufficient

除了下载和解析网站所需的每个 css 和 javascript 脚本并使用正则表达式查找图像文件之外,还有什么更聪明的做法吗?

如果有一种方法可以在页面加载后只查找下载的资源,那将是理想的,类似于中的network选项卡chrome dev tools


http://img1.mukewang.com/63a125060001fb0725601109.jpg

largeQ
浏览 159回答 1
1回答

ABOUTYOU

答案最初取自How to access Network panel on google chrome developer tools with selenium? . 我刚刚更新了一点。resources = driver.execute_script("return window.performance.getEntriesByType('resource');")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for resource in resources:&nbsp;&nbsp; &nbsp; if resource['initiatorType'] == 'img': # check for other types if needed&nbsp; &nbsp; &nbsp; &nbsp; print(resource['name']) # this is the original link of the file
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python