如何下载网页的所有图片并将其保存为原始名称?
问题描述:
我编写了一个小的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:
- 下载网页上的所有图片吗?
- 不命名我下载的图像,而是保留其默认名称吗?
答
您需要切换到 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)