1,根据UIL下载图片/视频
def downForInterface(file_path): count = 1 value_rows = [] with open(file_path, encoding='UTF-8') as file: f_csv = csv.reader(file) for r in f_csv: value_rows.append(r) for file_path in value_rows: cunmulu = '' if '.' in file_path[0]: print(cunmulu + str(random.random()) + '.' + file_path[0].split('.')[-1]) urllib.request.urlretrieve(file_path[0], cunmulu + str(count) + '.' + file_path[0].split('.')[-1]) else: print(cunmulu + str(random.random()) + '.mp4') urllib.request.urlretrieve(file_path[0], cunmulu + str(count) + '.mp4') count = count + 1downForInterface('image_or_video_url.csv')
效果如如下:
image_or_video_url.csv文件内容案例如下:
http://p8.itc.cn/images01/20201106/58779d3abcf040429748ebef7c25b4bf.jpeg http://p9.itc.cn/images01/20201106/00bf12aff4c54f16b628097195a9bd6d.jpeg http://p8.itc.cn/images01/20201106/e4bd1a9946804c77b8ca38cb16494e5f.jpeg https://vd3.bdstatic.com/mda-nadbjpk0hnxwyndu/720p/h264_delogo/1642148105214867253/mda-nadbjpk0hnxwyndu.mp4 https://vd4.bdstatic.com/mda-pcraqjsn1bz1q2q0/sc/cae_h264/1679816509746997780/mda-pcraqjsn1bz1q2q0.mp4
2,根据URL自动下载图片/视频
import time
import pyautogui
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
class SaveImageOrVideo():
def __init__(self):
self.driver = webdriver.Chrome()
def saveImage(self,file_path):
a = 0
with open(file_path, "r") as file:
try:
for url in file.readlines():
a += 1
print(url)
self.driver.get(url)
time.sleep(2)
if(url.split('.')[-1].strip() not in 'mp4wmvrmaviflvwebmwavrmvbmpgmov'):
image_element = self.driver.find_element(By.XPATH,'/html/body/img')
else:
image_element = self.driver.find_element(By.XPATH,'/html/body/video')
action = ActionChains(self.driver).move_to_element(image_element)
action.context_click(image_element)
action.perform()
pyautogui.typewrite(['v'])
time.sleep(4)
pyautogui.typewrite(['enter'])
print("执行了{0}次,下载了{1}个文件".format(a, a))
time.sleep(500)
except Exception as err:
print('An exception happened:' + str(err))
finally:
self.driver.quit()
if __name__ == '__main__':
saveImageOrVideo = SaveImageOrVideo()
saveImageOrVideo.saveImage("image_or_video_url.csv")
效果图如下:
image_or_video_url.csv内容案例如下
http://p8.itc.cn/images01/20201106/58779d3abcf040429748ebef7c25b4bf.jpeg http://p9.itc.cn/images01/20201106/00bf12aff4c54f16b628097195a9bd6d.jpeg http://p8.itc.cn/images01/20201106/e4bd1a9946804c77b8ca38cb16494e5f.jpeg https://vd3.bdstatic.com/mda-nadbjpk0hnxwyndu/720p/h264_delogo/1642148105214867253/mda-nadbjpk0hnxwyndu.mp4 https://vd4.bdstatic.com/mda-pcraqjsn1bz1q2q0/sc/cae_h264/1679816509746997780/mda-pcraqjsn1bz1q2q0.mp4
3、GUI自动下载想要的图片
根据UIL下载图片/视频、根据URL自动下载图片/视频、GUI自动下载想要的图片篇结束,欢迎去我的主页查看其它关于技术的文章~~~