谷歌下载图片
github开源下载谷歌图片
https://github.com/hardikvasa/google-images-download
安装
pip install google_images_download
使用
googleimagesdownload -k “car” -sk ‘red,blue,white’ -l 10
k:表示关键字
-sk:具体描述
-l:表示限制下载图片张数
百度下载图片
import requests
请求目标网站:
requests.get(url)
打印状态码:
print(response.status_code) #正常是200
以文本形式打印网页源码
print(response.text)
以二进制形式打印网页源码
print(response.content)
返回string中所有与pattern相匹配的全部字串,返回形式为列表
import re
re.findall(pattern, string, flags=0)
#-- coding:utf-8 --
import re
import requests
import string
import os
import random
def dowmload_image(html, path,s_name):
image_url = re.findall(’“objURL”:"(.*?)",’, html, re.S)
print(‘len_image_url’,len(image_url))
print(‘image_url’,image_url)
i = 0
print(‘现在开始下载图片…’)
for each in image_url:
print(‘正在下载第’+str(i+1)+‘张图片,图片地址:’+str(each))
try:
image= requests.get(each, timeout=10)
except requests.exceptions.ConnectionError:
print(’【错误】当前图片无法下载’)
continue
image_path = path + ‘/’ + s_name+‘image’ + ‘-’ + str(i) + “.jpg”
with open(image_path,‘wb’) as fp:
# pic.content, You can also access the response body as bytes, for non-text requests
fp.write(image.content)
i += 1
print(‘下载完成’)
if name == ‘main’:
word = ‘广西摩托车车牌’
url = ‘http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=’+word+’&ct=201326592&v=flip’
#url = ‘https://www.google.com.hk/search?hl=zh-CN&tbm=isch&source=hp&biw=1430&bih=769&ei=wbFFW8rHD8auoAThqYqwBA&q=’ + word
cur_dir = os.getcwd()
#folder_name = ‘image’ + ‘’+ ‘’.join(random.sample(string.ascii_letters + string.digits, 8))
s_name=‘广西摩’
folder_name = ‘image’ + '’+ s_name
image_save_path = os.path.join(cur_dir, folder_name)
if not os.path.exists(image_save_path):
os.mkdir(image_save_path)
Response = requests.get(url)
print(Response.status_code)
#print(Response.text)
# Response.text, read the content of the server’s response
dowmload_image(Response.text, image_save_path,s_name)
print(‘图片保存在:’ + image_save_path)