资源简介
百度图片下载器,用python实现的,你可以点击exe文件运行,也可以用python解释器运行。
代码片段和文件信息
# -*- coding: utf-8 -*-
import re
import urllib
import requests
import os
import time
reg = “https://ss1.bdstatic.com/(.+?).jpg“
reg = re.compile(reg)
header = {‘User-Agent‘:
‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/61.0.3163.79 Safari/537.36‘
}
path = str(os.getcwd())+“\\picture\\“
if not os.path.exists(path):
os.mkdir(path)
print(“您即将下载的图片将被保存在:“+path+“目录。“)
#生成搜索链接
def getKeyUrl(keywordpage):
url = “https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=“
req_url = []
for i in range(020*page20):
req_url.append(url+urllib.request.quote(keyword)+ “&pn=“+str(i)+“&gsm=50&ct=&ic=0&lm=-1&width=0&height=0“)
return req_url
#获取每一页的图片链接
def getPageUrl(page_url):
req = requests.get(page_urlheaders = header)
txt = req.text
url = re.findall(regtxt)
urls = []
for i in url:
urls.append(“https://ss1.bdstatic.com/“+str(i)+“.jpg“)
return set(urls)
def download(req_url):
count = 0
img_number = 0
img_url = []
for i in req_url:
page_url = getPageUrl(i)
img_num = len(page_url)
print(“第 %s 页一共有 %s 张图片“ %(countpage_num))
print(“正在获取图片链接“)
for url in page_url:
print(“正在获取第 %s 张图片链接“ %img_number)
print(url)
#downloadPic(urlimg_number)
img_number+=1
img_url.append(url)
count +=1
flag = 0
total = len(img_url)
img_url = set(img_url)
print(“一共有 %s 张图片“ %total)
for i in img_url:
print(“正在下载第 %s 张图片“ %flag)
downloadPic(iflag)
flag = flag+1
def downloadPic(urlimg_number):
req = requests.get(urlheaders = header)
try:
req = requests.get(urlheaders = headertimeout=20)
string = path + str(img_number) + ‘.jpg‘
print(“正在下载: “+url)
fp = open(string‘wb‘)
fp.write(req.content)
fp.close()
except requests.exceptions.Connectionerror:
print (‘当前图片无法下载‘)
pass
if __name__ ==‘__main__‘:
start = time.time()
keyword = str(input(“请输入要搜索的百度图片关键字:“))
page_num = int(input(“请输入要下载的图片页数:“))
req_url = getKeyUrl(keywordpage_num)
list = getPageUrl(req_url[0])
download(req_url)
end = time.time()
cost = str(end-start)
print(“图片下载结束,一共用了 %s 秒,5秒后程序将自动退出“ %cost)
time.sleep(5)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2017703 2017-10-30 18:30 百度图片下载器\baiduspider.exe
文件 2708 2017-09-08 11:20 百度图片下载器\baiduspider.py
目录 0 2017-10-30 18:30 百度图片下载器
----------- --------- ---------- ----- ----
2020411 3
相关资源
- Python不可见水印隐形水印
- python3爬虫
- Python Data Analysis Cookbook by Ivan Idris
- Python 3.7.3 中文文档
- wxPython学习手册
- python 实现KML转换为Excel的工具
- 微信公众号第三方平台python完整开发
- 语义分割代码(网盘)
- Python基础教程第三版PDF高清可复制.
- 《深度学习Deep Learning with Python 2017》
- 《机器学习》第2章中候选消除CANDID
- Python编程:从入门到实践高清完整版
- python太空大战游戏 源代码完整版
- Python微信打飞机小游戏
- python实现用户画像
- Test-Driven Development with Python
- Mastering Python Networking
- Learning Data Mining with Python - Second Edit
- Beginning Python: From Novice to Professional
- Python for Everybody: Exploring Data in Python
- Python Projects for Kids
- Qt5 Python GUI Programming Cookbook 无水印原
- 人脸识别算法 python
- Python3实现KNN的三个包含数据集,水果
- 廖雪峰python教程打印版完整版.pdf
- Python项目案例开发从入门到实战源代
- Twisted-17.9.0.tar.bz2和setuptools-19.6.tar.g
- XBEE Python Library
- OpenCV官方教程中文版Python版带完整书
- dlib18.17 编译好的python-dlib库 不需要
评论
共有 条评论