一:用selenium-wire和requests实现单张百度图片下载
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from seleniumwire.webdriver import Chrome
import requests
driver = Chrome(options= Options())
driver.get("https://www.baidu.com/")
driver.maximize_window()
time.sleep(3)
#右键点击图片,在新的页面打开,可以在地址栏查看到图片地址和图片名称,当前图片名称为:PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png
#利用selenium-wire的功能,将接口返回的内容放进itemlist中,遍历url,查找接口中包含图片名称url(如找不到,则报错),
results = [item for item in driver.requests
if 'PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png' in item.url]
if len(results) == 0:
raise Exception('没有找到百度图片!')
#将找到的图片保存,默认保存在程序运行的文件夹中
with open('baidu.png', 'wb') as f:
f.write(results[0].response.body)
二:用selenium-wire和requests下载天猫商城图片列表中的全部图片
用‘/’分割图片url,截图最后一段作为图片名称.png
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from seleniumwire.webdriver import Chrome
import requests
driver = Chrome(options= Options())
driver.get("https://www.tmall.com/")
driver.maximize_window()
time.sleep(3)
#获取图片list,图片块,图片img
imgModule = driver.find_element(By.XPATH,"//div[@class ='rax-view-v2 FloorModule--tmcsModule--27mV5Wq']")
imgContentItems = imgModule.find_elements(By.XPATH,"./div[2]/div")
# 滚动条滑动至图片列表最后一张图片处
driver.execute_script('arguments[0].scrollIntoView(false);', imgContentItems[-1])
#遍历获取所有图片
for img in imgContentItems:
#拿到图片的url
img_url_ele = (img.find_element(By.XPATH, "./a/img"))
img_url = img_url_ele.get_attribute('src')
split_list = img_url.split('/')
# 用‘/’分割图片url,截图最后一段作为图片名称
img_name = split_list[-1]
r=requests.get(img_url)
#下载图片
with open(img_name, 'wb') as f:
f.write(r.content)
driver.close()