最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python獲取網(wǎng)絡(luò)圖片和視頻的示例代碼

 更新時(shí)間:2022年03月14日 08:39:09   作者:求則得之,舍則失之  
Python 是一種多用途語(yǔ)言,廣泛用于腳本編寫。我們可以編寫Python 腳本來(lái)自動(dòng)化日常事務(wù)。本文將用Python實(shí)現(xiàn)獲取Google圖片和YouTube視頻,需要的可以參考一下

1.網(wǎng)絡(luò)獲取Google圖像

1.1 google_images_download

Python 是一種多用途語(yǔ)言,廣泛用于腳本編寫。我們可以編寫 Python 腳本來(lái)自動(dòng)化日常事務(wù)。假設(shè)我們要下載具有多個(gè)搜索查詢的谷歌圖片。我們可以自動(dòng)化該過(guò)程,而不是手動(dòng)進(jìn)行。

如何安裝所需的模塊:

pip install google_images_download

讓我們看看如何編寫 Python 腳本以使用 Python google_images_download 模塊下載 Google 圖像。

# importing google_images_download module
from google_images_download import google_images_download

# creating object
response = google_images_download.googleimagesdownload()

search_queries =
[
'The smartphone also features an in display fingerprint sensor.',
'The pop up selfie camera is placed aligning with the rear cameras.',
'''In terms of storage Vivo V15 Pro could offer
up to 6GB of RAM and 128GB of onboard storage.''',
'The smartphone could be fuelled by a 3 700mAh battery.',
]


def downloadimages(query):
	# keywords is the search query
	# format is the image file format
	# limit is the number of images to be downloaded
	# print urs is to print the image file url
	# size is the image size which can
	# be specified manually ("large, medium, icon")
	# aspect ratio denotes the height width ratio
	# of images to download. ("tall, square, wide, panoramic")
	arguments = {"keywords": query,
				"format": "jpg",
				"limit":4,
				"print_urls":True,
				"size": "medium",
				"aspect_ratio":"panoramic"}
	try:
		response.download(arguments)
	
	# Handling File NotFound Error	
	except FileNotFoundError:
		arguments = {"keywords": query,
					"format": "jpg",
					"limit":4,
					"print_urls":True,
					"size": "medium"}
					
		# Providing arguments for the searched query
		try:
			# Downloading the photos based
			# on the given arguments
			response.download(arguments)
		except:
			pass

# Driver Code
for query in search_queries:
	downloadimages(query)
	print()

輸出

注意:由于下載錯(cuò)誤,部分圖片無(wú)法打開(kāi)。

1.2 BeautifulSoup

import re
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import os
f = open("images_flowers.txt", "w")
res=[]
def download_google(url):
    #url = 'https://www.google.com/search?q=flowers&sxsrf=ALeKk00uvzQYZFJo03cukIcMS-pcmmbuRQ:1589501547816&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjEm4LZyrTpAhWjhHIEHewPD1MQ_AUoAXoECBAQAw&biw=1440&bih=740'
    page = requests.get(url).text
    soup = BeautifulSoup(page, 'html.parser')

    for raw_img in soup.find_all('img'):
        link = raw_img.get('src')
        res.append(link)
        if link:
            f.write(link +"\n")


download_google('https://www.google.com/search?q=flowers&sxsrf=ALeKk00uvzQYZFJo03cukIcMS-pcmmbuRQ:1589501547816&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjEm4LZyrTpAhWjhHIEHewPD1MQ_AUoAXoECBAQAw&biw=1440&bih=740')

f.close()

1.3 pyimagesearch

感謝 Adrian Rosebrock 編寫此代碼并將其公開(kāi)。

# USAGE
# python download_images.py --urls urls.txt --output images/santa

# import the necessary packages
from imutils import paths
import argparse
import requests
import cv2
import os

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--urls", required=True,
	help="path to file containing image URLs")
ap.add_argument("-o", "--output", required=True,
	help="path to output directory of images")
args = vars(ap.parse_args())

# grab the list of URLs from the input file, then initialize the
# total number of images downloaded thus far
rows = open(args["urls"]).read().strip().split("\n")
total = 0

# loop the URLs
for url in rows:
	try:
		# try to download the image
		r = requests.get(url, timeout=60)

		# save the image to disk
		p = os.path.sep.join([args["output"], "{}.jpg".format(
			str(total).zfill(8))])
		f = open(p, "wb")
		f.write(r.content)
		f.close()

		# update the counter
		print("[INFO] downloaded: {}".format(p))
		total += 1

	# handle if any exceptions are thrown during the download process
	except:
		print("[INFO] error downloading {}...skipping".format(p))

# loop over the image paths we just downloaded
for imagePath in paths.list_images(args["output"]):
	# initialize if the image should be deleted or not
	delete = False

	# try to load the image
	try:
		image = cv2.imread(imagePath)

		# if the image is `None` then we could not properly load it
		# from disk, so delete it
		if image is None:
			print("None")
			delete = True

	# if OpenCV cannot load the image then the image is likely
	# corrupt so we should delete it
	except:
		print("Except")
		delete = True

	# check to see if the image should be deleted
	if delete:
		print("[INFO] deleting {}".format(imagePath))
		os.remove(imagePath)

2.網(wǎng)絡(luò)獲取Youtube視頻

如何安裝所需的模塊:

pip install pytube3
import cv2
from collections import defaultdict

import matplotlib.pyplot as plt 
import numpy as np
import pandas as pd
import warnings
from pytube import YouTube


warnings.filterwarnings('ignore')

video = YouTube('https://www.youtube.com/watch?v=GTkU4qj6v7g')
# print(video.streams.all())
print(video.streams.filter(file_extension = "mp4").all())
# [<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">,
# <Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">,
# <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.64001f" progressive="False" type="video">,
# <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
# <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d4015" progressive="False" type="video">,
# <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d400d" progressive="False" type="video">,
# <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d400c" progressive="False" type="video">,
# <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400b" progressive="False" type="video">,
# <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">]

# 為要下載的視頻的分辨率使用適當(dāng)?shù)?itag。如果您需要高分辨率視頻下載,
# 請(qǐng)?jiān)谝韵虏襟E中選擇最高分辨率的 itag 進(jìn)行下載
print(video.streams.get_by_itag(137).download())
# '/Users/sapnasharma/Documents/github/video_clips/Akshay Kumars Fitness Mantras for a Fit India  GOQii Play Exclusive.mp4'
video_path = video.title
print(video_path)
# "Akshay Kumar's Fitness Mantras for a Fit India | GOQii Play Exclusive"

# 視頻標(biāo)題在名稱之間添加了一個(gè)管道,因此實(shí)際名稱已損壞。我稍后會(huì)修復(fù)這個(gè)錯(cuò)誤,
# 現(xiàn)在我們可以直接粘貼視頻的名字來(lái)達(dá)到我們的目的。
video_path = "Akshay Kumars Fitness Mantras for a Fit India  GOQii Play Exclusive.mp4"
# Video Capture Using OpenCV
cap = cv2.VideoCapture(video_path)

frame_cnt = int(cap.get(cv2.cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)

print('Frames in video: ', frame_cnt)
print(f"Frames per sec: {fps}")
# Frames in video:  34249
# Frames per sec: 25.0
# (1)要獲取整個(gè)視頻的幀,請(qǐng)使用下面的代碼塊。
# Use this for accessing the entire video
index = 1

for x in range(frame_cnt):
    ret, frame = cap.read()    
    if not ret:
        break
        
    # Get frame timestamp
    frame_timestamp = cap.get(cv2.CAP_PROP_POS_MSEC)
    
    # fetch frame every sec
    if frame_timestamp >= (index * 1000.0): # change the value from 1000 to anyother value if not needed per second
        index = index + 2   # decides the freq. of frames to be saved
        print(f"++ {index}")
        cv2.imwrite(f"images/cv_{index}.png", frame)   
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()


# (2)要獲取特定持續(xù)時(shí)間之間的幀,請(qǐng)使用以下代碼塊。
# Use this in case frames are to be fetched within a certain time frame
# frame_timestamp will be calculated as fps*time*1000 and set the starting index accordingly
index = 1560

for x in range(frame_cnt):
    ret, frame = cap.read()
    
    if not ret:
        break
        
    # Get frame timestamp
    frame_timestamp = cap.get(cv2.CAP_PROP_POS_MSEC)
    if frame_timestamp >= 1560000.0 and frame_timestamp <= 1800000.0 :
        # fetch frame every sec
        if frame_timestamp >= (index * 1000.0):
            index = index + 4   # decides the freq. of frames to be saved
            print(f"++ {index}")
            
            
            cv2.imwrite(f"images/cv_{index}.png", frame)
    
    
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()

以上就是Python獲取網(wǎng)絡(luò)圖片和視頻的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python獲取圖片 視頻的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python中執(zhí)行JavaScript實(shí)現(xiàn)數(shù)據(jù)抓取的多種方法

    Python中執(zhí)行JavaScript實(shí)現(xiàn)數(shù)據(jù)抓取的多種方法

    JavaScript是一門強(qiáng)大的腳本語(yǔ)言,廣泛應(yīng)用于網(wǎng)頁(yè)前端開(kāi)發(fā)、構(gòu)建交互式用戶界面以及處理各種客戶端端任務(wù),有時(shí)可能需要在Python環(huán)境中執(zhí)行JavaScript代碼,本文將介紹多種方法,幫助你在Python中執(zhí)行 JavaScript代碼,并提供詳盡的示例代碼,使你能夠輕松掌握這一技能
    2023-11-11
  • python基礎(chǔ)之內(nèi)置函數(shù)

    python基礎(chǔ)之內(nèi)置函數(shù)

    這篇文章主要介紹了python內(nèi)置函數(shù),實(shí)例分析了Python中返回一個(gè)返回值與多個(gè)返回值的方法,需要的朋友可以參考下
    2021-10-10
  • 如何使用python的xml庫(kù)實(shí)現(xiàn)自閉合標(biāo)簽

    如何使用python的xml庫(kù)實(shí)現(xiàn)自閉合標(biāo)簽

    文章介紹了作者編寫一個(gè)URDF格式化插件的初衷,目的是解決sw2urdf導(dǎo)出的URDF文件格式混亂的問(wèn)題,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-01-01
  • Python實(shí)現(xiàn)對(duì)二維碼數(shù)據(jù)進(jìn)行壓縮

    Python實(shí)現(xiàn)對(duì)二維碼數(shù)據(jù)進(jìn)行壓縮

    當(dāng)前二維碼的應(yīng)用越來(lái)越廣泛,包括疫情時(shí)期的健康碼也是應(yīng)用二維碼的典型案例。本文的目標(biāo)很明確,就是使用python,實(shí)現(xiàn)一張二維碼顯示更多信息,代碼簡(jiǎn)單實(shí)用,感興趣的可以了解一下
    2023-02-02
  • Django Rest framework之權(quán)限的實(shí)現(xiàn)示例

    Django Rest framework之權(quán)限的實(shí)現(xiàn)示例

    這篇文章主要介紹了Django Rest framework之權(quán)限的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • 提高Python生產(chǎn)力的五個(gè)Jupyter notebook插件

    提高Python生產(chǎn)力的五個(gè)Jupyter notebook插件

    Jupyter Notebook 因其可用性和實(shí)用性而成為數(shù)據(jù)分析和機(jī)器學(xué)習(xí)模型領(lǐng)域最流行的 IDE,它也是很多數(shù)據(jù)初學(xué)者的首選 IDE。它最具特色的是,擁有豐富的插件、擴(kuò)展數(shù)據(jù)處理能力和提升工作效率
    2021-11-11
  • Python實(shí)現(xiàn)批量Excel拆分功能

    Python實(shí)現(xiàn)批量Excel拆分功能

    在日常辦公中,我們經(jīng)常需要將包含多個(gè)Sheet頁(yè)的Excel文件拆分成多個(gè)獨(dú)立的Excel文件,下面我們就來(lái)看看如何使用Python實(shí)現(xiàn)批量Excel拆分的功能吧
    2025-02-02
  • 詳解Python的Flask框架中的signals信號(hào)機(jī)制

    詳解Python的Flask框架中的signals信號(hào)機(jī)制

    這里將為大家來(lái)詳解Python的Flask框架中的signals信號(hào)機(jī)制,包括講述信號(hào)的用途,并給出創(chuàng)建信號(hào)、訂閱信號(hào)、發(fā)送信號(hào)的方法,需要的朋友可以參考下
    2016-06-06
  • 詳解Python調(diào)試神器之PySnooper

    詳解Python調(diào)試神器之PySnooper

    在程序開(kāi)發(fā)過(guò)程中,代碼的運(yùn)行往往會(huì)和我們預(yù)期的結(jié)果有所差別。于是,我們需要清楚代碼運(yùn)行過(guò)程中到底發(fā)生了什么?代碼哪些模塊運(yùn)行了,哪些模塊沒(méi)有運(yùn)行?輸出的局部變量是什么樣的?PySnooper,能夠大大減少調(diào)試過(guò)程中的工作量
    2021-11-11
  • Tensorflow tf.nn.depthwise_conv2d如何實(shí)現(xiàn)深度卷積的

    Tensorflow tf.nn.depthwise_conv2d如何實(shí)現(xiàn)深度卷積的

    這篇文章主要介紹了Tensorflow tf.nn.depthwise_conv2d如何實(shí)現(xiàn)深度卷積的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論

杭锦后旗| 淳化县| 丽江市| 钟山县| 陕西省| 方正县| 巢湖市| 小金县| 南昌市| 双流县| 梁山县| 任丘市| 玉树县| 大宁县| 丹寨县| 田林县| 鸡西市| 枣强县| 合作市| 扎囊县| 湖北省| 望都县| 台中市| 克拉玛依市| 仙桃市| 大洼县| 随州市| 留坝县| 正定县| 依兰县| 全南县| 麻栗坡县| 渝北区| 黄浦区| 攀枝花市| 宁晋县| 柳河县| 塔河县| 汨罗市| 大冶市| 江门市|