由于人脸识别项目需要,需在百度图片爬取人脸数据并将获取到的文件名称归一化处理,就像这样:
解决方法:
Python 3.7
闪亮登场:spider.py
# coding=utf-8
"""根据搜索词下载百度图片"""
import re
import sys
import urllib
import requests
def getPage(keyword,page,n):
page=page*n
keyword=urllib.parse.quote(keyword, safe='/')
url_begin= "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word="
url = url_begin+ keyword + "&pn=" +str(page) + "&gsm="+str(hex(page))+"&ct=&ic=0&lm=-1&width=0&height=0"
return url
def get_onepage_urls(onepageurl):
try:
html = requests.get(onepageurl).text
except Exception as e:
print(e)
pic_urls = []
return pic_urls
pic_urls = re.findall('"objURL":"(.*?)",', html, re.S)
return pic_urls
def down_pic(pic_urls):
"""给出图片链接列表, 下载所有图片"""
for i, pic_url in enumerate(pic_urls):
try:
pic = requests.get(pic_url, timeout=15)
string =str(i + 1) + '.jpg'
with open(string, 'wb') as f:
f.write(pic.content)
print('成功下载第%s张图片: %s' % (str(i + 1), str(pic_url)))
except Exception as e:
print('下载第%s张图片时失败: %s' % (str(i + 1), str(pic_url)))
print(e)
continue
if __name__ == '__main__':
keyword = '王承渲青春有你' # 关键词, 改为你想输入的词即可, 相当于在百度图片里搜索一样
page_begin=0
page_number=30
image_number=3
all_pic_urls = []
while 1:
if page_begin>image_number:
break
print("第%d次请求数据",[page_begin])
url=getPage(keyword,page_begin,page_number)
onepage_urls= get_onepage_urls(url)
page_begin += 1
all_pic_urls.extend(onepage_urls)
down_pic(list(set(all_pic_urls)))
Dos
脚本:自动jpg重命名.bat
@echo off
set a=0
setlocal EnableDelayedExpansion
for %%n in (*.jpg) do (
set /A a+=1
ren "%%n" "!a!.png"
)
自动jpg重命名.bat
@echo off
set a=0
setlocal EnableDelayedExpansion
for %%n in (*.png) do (
set /A a+=1
ren "%%n" "!a!.jpg"
)
前者作用:同目录下的所有jpg
文件自动按照1,2,3...的顺序保存为png
格式;
后者作用:同目录下的所有png
文件自动按照1,2,3...的顺序保存为jpg
格式。
参考资料:
https://blog.csdn.net/lewky_liu/article/details/80490585
https://blog.csdn.net/hust_bochu_xuchao/article/details/79431145
版权属于:soarli
本文链接:https://blog.soarli.top/archives/396.html
转载时须注明出处及本声明。