大佬教程收集整理的这篇文章主要介绍了如何在python中使用BS4从谷歌图像中抓取高分辨率图像,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我们制作了一个程序,它通过 tkinter GUI 接受输入并转到谷歌图像,并根据输入下载图像。代码如下:
import requests
import bs4
import random
from PIL import Image
from tkinter import messageBox as msgBox
i=0
import os
from tkinter import *
from tkinter import filedialog
ac=str(random.randint(1,20))
b=str(random.randint(20,38))
y=Tk()
def find_file():
aaa=filedialog.askdirectory()
return aaa
def create_folder():
ad=find_file()
global ac
global b
ads=os.path.join(ad,f"img{ac}{B}")
os.mkdir(ads)
return ads
defe=Entry(bg="white")
defe.grID(row=2,column=2)
adj=Label(text="Enter the name of the photo(s) you want to download :")
adj.grID(row=2,column=1)
ack=Label(text="How many photos you want to download?")
ack.grID(row=3,column=1)
dee=Entry(bg="white")
dee.grID(row=3,column=2)
def download_images():
defei=defe.get()
deee=int(dee.get())
aadgc=[]
play=True
if " gif" in defei or ".gif" in defei:
msgBox.showerror("GIF not supported",".gif format is not supported by this software.sorry for the inconvenIEnce")
play=false
while play:
asd=create_folder()
for start in range(0,400,20):
bararara=f"https://www.Google.co.in/search?q={defei}&@R_262_9016@e=lnms&tbm=isch&start={start}#imgrc=fTslNdnf0RRRxM"
a=requests.get(bararara).text
soup=bs4.beautifulSoup(a,"lxml")
ab=soup.find_all("img",{"class":"n3VNCb"},limit=deeE)
aadgc.extend(ab)
aa=[abb["src"] for abb in aadgc]
for @R_262_9016@e in aa:
r=random.randint(0,100)
ra=random.randint(0,1000)
raa=asd+"\\"+str(r)+str(ra)+".png"
try:
binary=requests.get(@R_262_9016@E).content
except requests.exceptions.MissingscheR_759_11845@a:
binary=requests.get("http:"+@R_262_9016@E).content
except:
binary=requests.get("https:"+@R_262_9016@E).content
with open(raa,"wb") as saaho:
saaho.write(binary)
saaho.close()
global i
i+=1
if i==int(deeE):
break
asd=asd.replace("/","\\")
os.system(f"explorer \"{asD}\"")
break
aadg=button(y,bg="red",text="Download!",command=lambda:download_images(),activeBACkground="dark red",activeforeground="grey")
aadg.grID(row=4,column=1)
y.mainloop()
aadg=button(y,column=1)
y.mainloop()
但是我们获取的是图像的缩略图而不是图像,因为软件只返回低分辨率的照片,不支持 .gif 图像。
我们也无法找到主图像所属的类。 谢谢。
使用硒:
点击搜索结果中的图片。
等到图像可见。
image_link = driver.find_elemenT_By_css_SELEctor(".tvh9oe.bIB1wf .eHAdSb>img").get_attribute("src")
您可以对 bs4
使用相同的定位器
要找到原始图像或全分辨率图像,您必须先获取图像的 data-tbnid
。
在本例中为:sd7iKvYzujke_M
。获得ID后,您只需要使用正则表达式从页面源中提取完整的原始图像即可。
或者,您可以使用第三方解决方案,例如 SerpApi。这是一个免费试用的付费 API。
from serpapi import GoogleSearch
params = {
"api_key": "secret_api_key","ENGIne": "google","q": "inception","tbm": "isch"
}
search = GoogleSearch(params)
results = search.get_Dict()
示例 JSON 输出:
"images_results": [
{
"position": 1,"thumbnail": "https://serpapi.com/searches/60e70bf0e815af01fd163d6a/images/39eac787b1522b4ccc71382ac53fc933e15aa52342a5d06fafca53990897f2f9.jpeg","@R_262_9016@e": "rottentomatoes.com","title": "Inception (2010) - Rotten Tomatoes","link": "https://www.rottentomatoes.com/m/inception","original": "https://flxt.tmsimg.com/assets/P7825626_p_v10_af.jpg"
},{
"position": 2,"thumbnail": "https://serpapi.com/searches/60e70bf0e815af01fd163d6a/images/39eac787b1522b4ce9c6c6de6d244df2098ae0b7da6856fba545b4376e01d075.jpeg","@R_262_9016@e": "imdb.com","title": "Inception (2010) - IMDb","link": "https://www.imdb.com/title/tt1375666/","original": "https://m.media-amazon.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_.jpg"
},{
"position": 3,"thumbnail": "https://serpapi.com/searches/60e70bf0e815af01fd163d6a/images/39eac787b1522b4ca9d2321fc39291764c3894c9816af84a5af355f2d54f6921.jpeg","@R_262_9016@e": "screenrant.com","title": "Inception: what Each Character Represents (Confirmed By Christopher Nolan)","link": "https://screenrant.com/inception-movie-christopher-nolan-characters-actors-meaning-confirmed/","original": "https://static2.srcdn.com/wordpress/wp-content/uploads/2020/03/Inception-characters-film-crew.jpg?q=50&fit=crop&w=960&h=500&dpr=1.5"
},...
]
查看documentation了解更多详情。
免责声明:我在 SerpApi 工作。
以上是大佬教程为你收集整理的如何在python中使用BS4从谷歌图像中抓取高分辨率图像全部内容,希望文章能够帮你解决如何在python中使用BS4从谷歌图像中抓取高分辨率图像所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。