欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

1.1 google_images_download

Python 是一種多用途語言,廣泛用于腳本編寫。我們可以編寫 Python 腳本來自動(dòng)化日常事務(wù)。假設(shè)我們要下載具有多個(gè)搜索查詢的谷歌圖片。我們可以自動(dòng)化該過程,而不是手動(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ò)誤,部分圖片無法打開。

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 編寫此代碼并將其公開。

# 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。如果您需要高分辨率視頻下載,
# 請?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)在我們可以直接粘貼視頻的名字來達(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è)視頻的幀,請使用下面的代碼塊。
# 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í)間之間的幀,請使用以下代碼塊。
# 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獲取圖片 視頻的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 利用Tensorflow的隊(duì)列多線程讀取數(shù)據(jù)方式

    利用Tensorflow的隊(duì)列多線程讀取數(shù)據(jù)方式

    今天小編就為大家分享一篇利用Tensorflow的隊(duì)列多線程讀取數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • linux下python中文亂碼解決方案詳解

    linux下python中文亂碼解決方案詳解

    這篇文章主要介紹了linux下python中文亂碼解決方案詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python實(shí)現(xiàn)Mysql全量數(shù)據(jù)同步的腳本分享

    Python實(shí)現(xiàn)Mysql全量數(shù)據(jù)同步的腳本分享

    這篇文章主要為大家詳細(xì)介紹了基于Python如何實(shí)現(xiàn)Mysql全量數(shù)據(jù)同步的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • pymongo中聚合查詢的使用方法

    pymongo中聚合查詢的使用方法

    這篇文章主要給大家介紹了關(guān)于pymongo中聚合查詢的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用pymongo具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python使用turtle庫繪制時(shí)鐘

    python使用turtle庫繪制時(shí)鐘

    這篇文章主要為大家詳細(xì)介紹了python使用turtle庫繪制時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 阿里云ECS服務(wù)器部署django的方法

    阿里云ECS服務(wù)器部署django的方法

    今天小編就為大家分享一篇阿里云ECS服務(wù)器部署django的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • django 連接數(shù)據(jù)庫出現(xiàn)1045錯(cuò)誤的解決方式

    django 連接數(shù)據(jù)庫出現(xiàn)1045錯(cuò)誤的解決方式

    這篇文章主要介紹了django 連接數(shù)據(jù)庫出現(xiàn)1045錯(cuò)誤的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python 必須了解的5種高級特征

    Python 必須了解的5種高級特征

    Python 多好用不用多說,大家看看自己用的語言就知道了。但是 Python 隱藏的高級功能你都 get 了嗎?本文中,作者列舉了 Python 中五種略高級的特征以及它們的使用方法,快來一探究竟吧!
    2020-09-09
  • 使用Python內(nèi)置模塊與函數(shù)進(jìn)行不同進(jìn)制的數(shù)的轉(zhuǎn)換

    使用Python內(nèi)置模塊與函數(shù)進(jìn)行不同進(jìn)制的數(shù)的轉(zhuǎn)換

    這篇文章主要介紹了使用Python內(nèi)置模塊與函數(shù)進(jìn)行不同進(jìn)制的數(shù)的轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Keras中Conv1D的使用及說明

    Keras中Conv1D的使用及說明

    這篇文章主要介紹了Keras中Conv1D的使用及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12

最新評論