图片爬取的代码并非全是自己编写的,主要是参考借鉴他人的代码,进行稍微改动使其能正常运行,达到自己想要的效果。
这里将参考链接附上,并附上自己能成功运行的代码。
一:百度图片爬取
代码主要参考链接:https://www.jianshu.com/p/690a49eca5de
链接有百度图片网页的分析。
下面主要是我改动后能成功运行的代码:
# 爬取百度图片 输入关键字 瀑布流
import requests
import re
import time
import os
import urllib.parse
import json
page_picture_num=30
photo_dir="F://Tensorflow-workspace//photos_11" # 图片保存的路径
def getImage(word, page_num):
num=0
url = "https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={0}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=©right=&word={0}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn={1}&rn="+str(page_picture_num)+"&gsm=1e&1552975216767="
while num
二:Veer图库图片爬取
代码主要参考链接:https://blog.csdn.net/jklcl/article/details/82870362#comments
链接有Veer图库网页的分析。
原链接中的代码会出错,添加Cookie后能成功运行。
找到Cookie 中 latest_landing_page 和 22https 将中间的内容替换为: latest_landing_page'+str(i)+'22https
str(i) 为加载的 最后页数。
下面主要是我改动后能成功运行的代码(根据关键字、爬取相关页数的图片,每页200张图片):
注意:Cookie 请换成自己的Cookie。打开Veer 首页,输入关键字(爬取图片的关键字,如:站 小孩),按F12,找到Network,如下图所示:
代码实现:
# 爬取 Veer 图片网站 输入关键字 和 页数 进行爬取
#conding=utf-8
import requests
import json
import os
import traceback
photo_dir = r'F:\Tensorflow-workspace\photos_11' # 图片保存的路径
def download(img_url, img_name):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101'
}
req = requests.get(img_url, headers=headers)
file_name = word_dir + '\\' + img_name+img_url[-13:]#图片名为描述+图片的编号
f = open(file_name, 'wb')
f.write(req.content)#以字节流的形式读入文件 图片保存到关键字的文件夹下
f.close
def get_picture(name, i):
url = 'https://www.veer.com/ajax/search' #URL
print("-----i: "+ str(i))
header = {
'content-type': 'application/json',
'Cookie': '请填写自己的Cookie',
'Host': 'www.veer.com',
'Origin': 'https://www.veer.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'
}
payloadData = {
'graphicalStyle': '', #类型:1.照片 2.插画 3.矢量i图 Nan为全部
'phrase': name, #搜索的关键字
'page': i, #分页数量
'perpage': 1000, #一页多少图片
'page_type': 6, #分页的格式,这个不重要
'favorid': ''
}# 分页数量为1,一页10000,是为了尽量只从一个页面上的到所有的list
html = requests.post(url, data=json.dumps(payloadData), headers=header) #payloadData要求用josn来进行解析,
print(html.status_code)
print(html.request.url)
list = json.loads(html.text)#获取json解析的list,不然全是乱码
data = list['data'] #封装成字典格式
print("总共搜索到图片:",data['totalCount'], "张图片")
id = data['list']
j = 0
for sid in id:
try:
# print(sid['oss400'], sid['cnTitle'])#用的是400的,为了veer的利益,不采用1600尺寸的图片
if sid['cnTitle']==None:#防止图片描述为空
sid['cnTitle'] = "none"
download(sid['oss400'], sid['cnTitle'])#进行下载
j = j+1
except Exception as e:
#这个是输出错误的具体原因,这步可以不用加str,输出
# print('repr(e):\t', repr(e)) #输出 repr(e): ZeroDivisionError('integer division or modulo by zero',)
#以下两步都是输出错误的具体位置的
# traceback.print_exc()
# print('traceback.format_exc():\n%s' % traceback.format_exc())
print('---Url Error: ', sid)
return j
def get_num_list(name, page_num):
picture_all = 0
name = name
for i in range(100, page_num+1):
picture_num = get_picture(name, i)
print("------------page_num:------",i," ----picture_num: ",picture_num)
picture_all = picture_all + picture_num
print("------------get_picture_all:",picture_all)
print('Done!')
if __name__ == '__main__':
name = input("输入搜索图片名称:")
word_dir=os.path.join(photo_dir,name)
if not os.path.exists(word_dir):
os.mkdir(word_dir) # 在图片保存路径下 创建关键字的文件夹
page_num = input("请输入爬取的页数:")
while(page_num == "") :
print("爬取的页数不能为空")
page_num = input("请输入爬取的页数(每页200张图片):")
else:
page_num = int(page_num)
get_num_list(name, page_num)