目錄
1,根據(jù)UIL下載圖片/視頻
2,根據(jù)URL自動(dòng)下載圖片/視頻
3、GUI自動(dòng)下載想要的圖片
①點(diǎn)擊下載按鈕,進(jìn)行挨個(gè)下載
②右擊保存進(jìn)行下載圖片
4、圖片或視頻URL批量放入瀏覽器頁(yè)面上
1,根據(jù)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文件內(nèi)容案例如下:
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,根據(jù)URL自動(dòng)下載圖片/視頻 ?
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("執(zhí)行了{(lán)0}次,下載了{(lán)1}個(gè)文件".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內(nèi)容案例如下
?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自動(dòng)下載想要的圖片
①點(diǎn)擊下載按鈕,進(jìn)行挨個(gè)下載
#左鍵點(diǎn)擊下載圖片
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
def downImageLifeClick(keyWord,count):
before_time = time.time()
keyWord = keyWord
prefixUrl = "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word="
suffixUrl = "&step_word=&hs=2&pn=1&spn=0&di=13200&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=113014737%2C3445157660&os=2351244306%2C2367448695&simid=3050896469%2C3730470527&adpicid=0&lpn=0&ln=362&fr=&fmq=1570618921319_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined©right=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fgss0.baidu.com%2F-vo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F0df431adcbef7609968039362cdda3cc7dd99e94.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bp7xt_z%26e3Bv54_z%26e3BvgAzdH3Fetjof-8nbml9nnclam-8nbml9nnclamdbdd_z%26e3Bip4s&gsm=&rpstart=0&rpnum=0&islist=&querylist=&force=undefined"
driver = webdriver.Chrome()
driver.get(
prefixUrl+keyWord+suffixUrl)
try:
for i in range(0, count):
#下載操作
driver.maximize_window()
down = driver.find_element(By.XPATH,'//*[@id="toolbar"]/span[7]')
down.click()
time.sleep(1)
#翻頁(yè)操作
image = driver.find_element(By.XPATH,'//*[@id="container"]/span[2]/span')
image.click()
time.sleep(1)
print("已下載%d張圖片" % (i + 1))
except Exception as e:
print(e)
time.sleep(10000)
finally:
driver.quit()
after_time = time.time()
print('您一共花費(fèi)了%d秒' % (after_time - before_time))
downImageLifeClick('古怪搞笑圖',5)
效果圖如下:
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-647071.html
②點(diǎn)擊右鍵保存,進(jìn)行挨個(gè)下載
#pip install pypiwin32 -i https://pypi.douban.com/simple/
import win32api
import win32con
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
#右鍵保存下載圖片
from selenium.webdriver.common.by import By
def downImageRightClick(keyWord,count):
before_time = time.time()
keyWord = keyWord
prefixUrl = "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word="
suffixUrl = "&step_word=&hs=2&pn=1&spn=0&di=13200&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=113014737%2C3445157660&os=2351244306%2C2367448695&simid=3050896469%2C3730470527&adpicid=0&lpn=0&ln=362&fr=&fmq=1570618921319_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined©right=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fgss0.baidu.com%2F-vo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F0df431adcbef7609968039362cdda3cc7dd99e94.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bp7xt_z%26e3Bv54_z%26e3BvgAzdH3Fetjof-8nbml9nnclam-8nbml9nnclamdbdd_z%26e3Bip4s&gsm=&rpstart=0&rpnum=0&islist=&querylist=&force=undefined"
driver = webdriver.Chrome()
VK_CODE = {'enter': 0x0D, 'down_arrow': 0x28}
driver.get(
prefixUrl+keyWord+suffixUrl)
try:
for i in range(0,count):
image = driver.find_element(By.XPATH,'//*[@id="srcPic"]/img')
action = ActionChains(driver).move_to_element(image)
# ActionChains(driver).context_click(image).perform()
action.context_click(image).perform()
time.sleep(1)
win32api.keybd_event(86, 0, 0, 0)
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2)
win32api.keybd_event(VK_CODE['enter'], 0, 0, 0)
win32api.keybd_event(VK_CODE['enter'], 0, win32con.KEYEVENTF_KEYUP, 0)
print("已下載%d張圖片" % (i + 1))
time.sleep(1)
driver.find_element(By.XPATH,'//*[@id="container"]/span[2]').click()
time.sleep(1)
except Exception as e:
print(e)
finally:
driver.quit()
after_time = time.time()
print('您一共花費(fèi)了%d秒' % (after_time - before_time))
downImageRightClick('清涼圖',5)
效果圖如下:
?
4、圖片或視頻URL批量放入瀏覽器頁(yè)面上
import time from selenium import webdriver def new_table(): driver = webdriver.Chrome() try: driver.maximize_window() driver.delete_all_cookies() fo = open("image_or_video_url.csv", "r") for line in fo.readlines(): if len(line) > 0: driver.get(line) driver.execute_script("window.open('');") # 打開(kāi)新的頁(yè)面 current_window = driver.current_window_handle handles = driver.window_handles # for handle in handles: # if current_window != handle: # driver.switch_to.window(handle) #耗時(shí) 50 185 # driver.switch_to.window(handles[len(handles)-1]) #耗時(shí) 50 101 driver.switch_to.window(handles[-1]) # 耗時(shí) 50 102 fo.close() time.sleep(500) except Exception as e: print(e) finally: driver.quit() new_table()
效果圖如下:
?
根據(jù)UIL下載圖片/視頻、根據(jù)URL自動(dòng)下載圖片/視頻、GUI自動(dòng)下載想要的圖片篇結(jié)束,歡迎去我的主頁(yè)查看其它關(guān)于技術(shù)的文章~~~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-647071.html
到了這里,關(guān)于根據(jù)UIL下載圖片/視頻、根據(jù)URL自動(dòng)下載圖片/視頻、GUI自動(dòng)下載想要的圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!