如何下载网页的所有图片并将其保存为原始名称? [英] How to download ALL the pictures of a webpage and save them in their original names?

查看:280
本文介绍了如何下载网页的所有图片并将其保存为原始名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个小的Python脚本,使用硒从网站上下载图片:

I coded a small Python script to download a picture from a website using selenium:

from selenium import webdriver
import urllib.request
class FirefoxTest:
    def firefoxTest(self):
        self.driver=webdriver.Firefox()
        self.driver.get("http://www.sitew.com")
        self.r=self.driver.find_element_by_tag_name('img')
        self.uri=self.r.get_attribute("src")
        self.g=urllib.request.urlopen(self.uri)
        with open("begueradj.png",'b+w') as self.f:
            self.f.write(self.g.read())             
if __name__=='__main__':
    FT=FirefoxTest()
    FT.firefoxTest()

如何修改我的代码,以便:

How can I modify my code in order to:

  1. 下载网页上的所有图片吗?
  2. 不命名我下载的图像,而是保留其默认名称吗?

推荐答案

您需要切换到 urllib.urlretrieve() -它会从中提取文件名您的网址:

You need to switch to find_elements_by_tag_name. For downloading files, I'd use urllib.urlretrieve() - it would extract the filename from the url for you:

images = self.driver.find_elements_by_tag_name('img')
for image in images:
    src = image.get_attribute("src")
    if src:
        urllib.urlretrieve(src)

这篇关于如何下载网页的所有图片并将其保存为原始名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆