我使用了question的第一个答案,以便根据我的需要进行调整:自动在笔记本电脑上保存给定网址的图片。
我的问题是如何获取网页上存在的每个图像的URI,以便正确完成我的代码:
import selenium
class TestFirefox:
def testFirefox(self):
self.driver=webdriver.Firefox()
# There are 2 pictures on google.com, I want to download them
self.driver.get("http://www.google.com")
self.l=[] # List to store URI to my images
self.r=self.driver.find_element_by_tag_name('img')
# I did print(self.r) but it does not reflect the URI of
# the image: which is what I want.
# What can I do to retrieve the URIs and run this:
self.l.append(self.image_uri)
for uri_to_img in self.l:
self.driver.get(uri_to_img)
# I want to download the images, but I am not sure
# if this is the good way to proceed since my list's content
# may not be correct for the moment
self.driver.save_screenshot(uri_to_image)
driver.close()
if __name__=='__main__':
TF=TestFirefox()
TF.testFirefox()
答案 0 :(得分:0)
您需要获取给定图像的get src
属性以确定其名称和(可能)地址 - 记住,src
也可以是相对URI。
for img in self.l:
url = img.get_attribute("src")
要下载图片,您应该尝试使用简单的HTTP客户端,例如urllib
import urllib.request
urllib.request.urlretrieve(url, "image.png")