爬虫

谷歌下载图片

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值