当前位置:网站首页>爬百度图片
爬百度图片
2022-08-08 16:54:00 【泉伟】
# -*- coding: utf-8 -*-
"""根据搜索词下载百度图片"""
import re
import sys
import urllib
import os
import requests
def get_onepage_urls(onepageurl):
"""获取单个翻页的所有图片的urls+当前翻页的下一翻页的url"""
if not onepageurl:
print('已到最后一页, 结束')
return [], ''
try:
html = requests.get(onepageurl)
html.encoding = 'utf-8'
html = html.text
except Exception as e:
print(e)
pic_urls = []
fanye_url = ''
return pic_urls, fanye_url
pic_urls = re.findall('"objURL":"(.*?)",', html, re.S)
fanye_urls = re.findall(re.compile(r'<a href="(.*)" class="n">下一页</a>'), html, flags=0)
fanye_url = 'http://image.baidu.com' + fanye_urls[0] if fanye_urls else ''
return pic_urls, fanye_url
def down_pic(pic_urls):
"""给出图片链接列表, 下载所有图片"""
save_path = './DataSet/' + keyword
if not os.path.exists(save_path):
print("Selected folder not exist, try to create it.")
os.makedirs(save_path)
for i, pic_url in enumerate(pic_urls):
try:
pic = requests.get(pic_url, timeout=5)
string = save_path + '/' + 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 = '晴天' # 关键词, 改为你想输入的词即可, 相当于在百度图片里搜索一样
url_init_first = r'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1497491098685_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1497491098685%5E00_1519X735&word='
url_init = url_init_first + urllib.parse.quote(keyword, safe='/')
all_pic_urls = []
onepage_urls, fanye_url = get_onepage_urls(url_init)
all_pic_urls.extend(onepage_urls)
fanye_count = 0 # 累计翻页数
while 1:
onepage_urls, fanye_url = get_onepage_urls(fanye_url)
fanye_count += 1
print('第%d页' % fanye_count)
if fanye_url == '' and onepage_urls == []:
break
all_pic_urls.extend(onepage_urls)
down_pic(list(set(all_pic_urls)))
如果需要下载百度图片时,仅仅是需要主程序第一行关键词修改为自己需要的关键词即可。
如果想详细了解爬虫需要的库以及详细过程可以参考的链接:python——爬虫示例读取豆瓣电影数据
边栏推荐
- 看到这个应用上下线方式,不禁感叹:优雅,太优雅了!
- L2-020 功夫传人 (25 分)
- bzoj1097 [POI2007]旅游景点atr
- Redis design and implementation notes (1)
- Es的索引操作(代码中的基本操作)
- Mysql都有那些最需要掌握的原理?
- 【LeetCode】试题总结:深度优先搜索 (DFS)
- ggplot2可视化水平箱图并使用fct_reorder排序数据、使用na.rm处理缺失值(reorder boxplot with fct_reorder)、按照箱图的中位数从大到小排序水平箱图
- 好用的项目工时管理系统有哪些
- 【MySQL哪些字段适合建索引,哪些查询条件会导致索引失效】
猜你喜欢

元宇宙医疗或将改变医疗格局

毕设-基于SSM学生考试系统

The latest research from PNAS: 81% problem solving rate, neural network Codex opens the door to the world of advanced mathematics

Appium 自动化测试环境搭建

Grid 布局介绍

JVM-简介&垃圾回收&内存泄漏分析

ERROR Failed to compile with 1 error

leetcode:306. 累加数
![[uniapp applet] view container cover-view](/img/88/664b62c87b1540f204e8ea4a810657.png)
[uniapp applet] view container cover-view

项目管理流程包含哪些
随机推荐
3dsmax2021软件安装教程
L2-009 抢红包 (25 分)(结构体+自定义排序)
Solve the inexplicable problem of MySQL violently - restart the service!
信号生成和可视化
[uniapp applet] view container cover-view
Appium 自动化测试环境搭建
L2-016 愿天下有情人都是失散多年的兄妹 (25 分)
2022年11大最佳缺陷管理工具盘点
用完华为云会议解决方案,我直接卸载了之前的会议软件【华为云至简致远】
L2-027 名人堂与代金券 (25 分)
iNFTnews | Metaverse brings new ideas for enterprise development
D. Districts Connection
bzoj1097 [POI2007]旅游景点atr
项目管理流程包含哪些
Spark cluster environment construction
ggplot2可视化水平箱图并使用fct_reorder排序数据、使用na.rm处理缺失值(reorder boxplot with fct_reorder)、按照箱图的中位数从大到小排序水平箱图
L2-022 重排链表 (25 分)(模拟链表)
LeetCode_Backtrack_Medium_491. Incrementing Subsequence
Lecture 207, Class Schedule
【LeetCode】试题总结:深度优先搜索 (DFS)