#!/usr/bin/env python3
#coding=utf-8
import re
import requests
import os
def dowmloadPic(html, keyword):
pic_url = re.findall('"objURL":"(.*?)",', html, re.S)
global num
print('find: ' + keyword + ' pictures ')
for each in pic_url:
print('Downloading' + str(num) + 'st url:' + str(each))
try:
pic = requests.get(each, headers=headers,timeout=10)
except requests.exceptions.ConnectionError:
print('Error this picture cant download')
continue
image_name = '%4d.jpg'%num
dir = os.path.join(outp_dir,image_name)
fp = open(dir, 'wb')
fp.write(pic.content)
fp.close()
num += 1
if __name__ == '__main__':
word = '马赛克的图片'
outp_dir = '/home/tarena/projects/'
#url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&ct=201326592&v=flip'
headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Mobile Safari/537.36'}
pagestart = 0
pageend = 100
num = 0
'''添加代理
proxies = {
http:'http://...'
https:'https://...'
}
'''
for idx in range(pagestart,pageend):
url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+word+'&pn=%d&gsm=3c&ct=&ic=0&lm=-1&width=0&height=0'%idx*20
#result = requests.get(url,headers=headers,proxies=proxies)
result = requests.get(url, headers=headers)
dowmloadPic(result.text, word)
if num >=1000:
break
python抓取百度图片
最新推荐文章于 2024-07-07 09:46:06 发布