根据搜索词抓取 Google 图片

Dhv*_*hah 1 python image beautifulsoup web-scraping

我希望抓取以下网址中显示的所有图像: happiness

我尝试了很多方法,但只能获取 20 张图像。下面是相同的 Python 代码:

query = input("happiness")# you can change the query for the image  here
image_type="ActiOn"
query= query.split()
query='+'.join(query)
url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
print(url)
#add the directory for your image here
DIR="Pictures"
header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"
}
soup = get_soup(url,header)
if not os.path.exists(DIR):
        os.mkdir(DIR)
DIR = os.path.join(DIR, query.split()[0])

if not os.path.exists(DIR):
        os.mkdir(DIR)

images = [a['src'] for a in soup.find_all("img", {"src": 
re.compile("gstatic.com")})]
print(images)
print("there are total" , len(images),"images")
image_type = "Action"
#print images
for img in images:
raw_img = urlopen(img).read()
#add the directory for your image here 
DIR="C:\\Users\\dhvani\\Pictures\\"+query+"\\"
cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
print(cntr)
f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb')
f.write(raw_img)
f.close()
Run Code Online (Sandbox Code Playgroud)

有人可以帮我提取所有图像吗?

jvm*_*vik 5

我们构建了一个解决方案来解决 Google 图片抓取问题。SerpAPI 是一个将 google 图像结果转换为 JSON 的 Web 服务。我们为所有最流行的平台提供扩展:Python、Ruby、Java、NodeJS 等...