python爬虫爬图片

爬虫爬baidu图片

第一步

载入爬虫模块

from requests_html import HTMLSession            #载入爬虫模块

第二步

创建session对象

from requests_html import HTMLSession            #载入爬虫模块
session =HTMLSession() #创建完毕

第三步

获得发现百度图片搜索规律并发起请求并匹配到图片的url

http://image.baidu.com/search/index?tn=baiduimage&fm=result&ie=utf-8&word=我们搜图片的关键字

from requests_html import HTMLSession            #载入爬虫模块
session =HTMLSession() #创建完毕
#拿二傻子为例
response = session.get('http://image.baidu.com/search/index?tn=baiduimage&fm=result&ie=utf-8&word=二傻子')
#获取我们图片的url的正则匹配格式
img_url_regex = '"thumbURL":"{}",'
#解析并获取图片url_list
img_url_list = response.html.search_all(img_url_regex)

第四步

访问图片url并且保存下来

from requests_html import HTMLSession            #载入爬虫模块
session =HTMLSession() #创建完毕
#拿二傻子为例
response = session.get('http://image.baidu.com/search/index?tn=baiduimage&fm=result&ie=utf-8&word=二傻子')
#获取我们图片的url的正则匹配格式
img_url_regex = '"thumbURL":"{}",'
#解析并获取图片url_list
img_url_list = response.html.search_all(img_url_regex) mun=0
for url in img_url_list:
mun+=1
#访问图片链接
response= session.get(url[0])
#保存二进制并保存至本地
with open(f'第{mun}张.jpg','wb') as fw:
fw.write(response.content)

第五步

类的封装

from requests_html import HTMLSession    

class BaiDuImg:
session = HTMLSession()
img_url_regex = '"thumbURL":"{}",'
url=''
img_url_list =[] def get_search(self):
search=input('请输入你要搜索的图片')
self.url=f'http://image.baidu.com/search/index?tn=baiduimage&fm=result&ie=utf-8&word={search}' def get_img_url_list(self):
response=self.session.get(self.url)
self.img_url_list = response.html.search_all(self.img_url_regex) def save_img(self):
mun = 0
for url in self.img_url_list:
mun += 1
# 访问图片链接
response = self.session.get(url[0])
# 保存二进制并保存至本地
with open(f'第{mun}张.jpg', 'wb') as fw:
fw.write(response.content) def run(self):
self.get_search()
self.get_img_url_list()
self.save_img() if __name__ == '__main__':
baidu=BaiDuImg()
baidu.run()

后来有个研一的小姐姐说要把全部爬完那就改改

from requests_html import HTMLSession

class BaiDuImg:
session = HTMLSession()
img_url_regex = '"thumbURL":"{}",'
url = ''
img_url_list = [] def get_search(self):
search = input('请输入你要搜索的图片')
#有点点偷懒参数没有好好分析全,只对关键参数处理
self.url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={search}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word={search}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&rn=30&gsm=' def get_img_url_list(self):
'&pn=30000'
pn = 0
try:
while True: #由于百度限制只能抓取450张,嗯可能能获取480张,我懒没接着分析了,如果真的需要私聊我我可以写全
res = self.session.get(f'{self.url}&pn={pn}')
print(res.json()['bdIsClustered'])
if res.json()['bdIsClustered']=='2':
break
else:
pn+=30
for dic in res.json()['data']:
img_url = dic.get('thumbURL')
if img_url:
self.img_url_list.append(img_url)
except Exception as e:
pass def save_img(self):
mun = 0
for url in self.img_url_list:
mun += 1
# 访问图片链接
response = self.session.get(url)
# 保存二进制并保存至本地
with open(f'第{mun}张.jpg', 'wb') as fw:
fw.write(response.content)
print(f'第{mun}张保存本地完毕') def run(self):
self.get_search()
self.get_img_url_list()
print(len(self.img_url_list))
self.save_img() if __name__ == '__main__':
baidu = BaiDuImg()
baidu.run()

python爬虫(爬取图片)的更多相关文章

  1. [python爬虫] 爬取图片无法打开或已损坏的简单探讨

    本文主要针对python使用urlretrieve或urlopen下载百度.搜狗.googto(谷歌镜像)等图片时,出现"无法打开图片或已损坏"的问题,作者对它进行简单的探讨.同时 ...

  2. 利用python爬虫爬取图片并且制作马赛克拼图

    想在妹子生日送妹子一张用零食(或者食物类好看的图片)拼成的马赛克拼图,因此探索了一番= =. 首先需要一个软件来制作马赛克拼图,这里使用Foto-Mosaik-Edda(网上也有在线制作的网站,但是我 ...

  3. Python 爬虫 爬取图片入门

    爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本. 用户看到的网页实质是由 HTML 代码构成的,爬 ...

  4. Spider-Python实战之通过Python爬虫爬取图片制作Win7跑车主题

    1. 前期准备 1.1 开发工具 Python 3.6 Pycharm Pro 2017.3.2 Text文本 1.2 Python库 requests re urllib 如果没有这些Python库 ...

  5. Python爬虫 - 爬取百度html代码前200行

    Python爬虫 - 爬取百度html代码前200行 - 改进版,  增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...

  6. 用Python爬虫爬取广州大学教务系统的成绩(内网访问)

    用Python爬虫爬取广州大学教务系统的成绩(内网访问) 在进行爬取前,首先要了解: 1.什么是CSS选择器? 每一条css样式定义由两部分组成,形式如下: [code] 选择器{样式} [/code ...

  7. 使用Python爬虫爬取网络美女图片

    代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install ...

  8. Python爬虫|爬取喜马拉雅音频

    "GOOD Python爬虫|爬取喜马拉雅音频 喜马拉雅是知名的专业的音频分享平台,用户规模突破4.8亿,汇集了有声小说,有声读物,儿童睡前故事,相声小品等数亿条音频,成为国内发展最快.规模 ...

  9. python爬虫爬取内容中,-xa0,-u3000的含义

    python爬虫爬取内容中,-xa0,-u3000的含义 - CSDN博客 https://blog.csdn.net/aiwuzhi12/article/details/54866310

  10. Python爬虫爬取一篇韩寒新浪博客

    网上看到大神对Python爬虫爬到非常多实用的信息,认为非常厉害.突然对想学Python爬虫,尽管自己没学过Python.但在网上找了一些资料看了一下,看到爬取韩寒新浪博客的视频.共三集,第一节讲爬取 ...

随机推荐

  1. self.tabBarController.selectedIndex

    KindViewController *vc =((UINavigationController *) [self.tabBarController viewControllers][]).viewC ...

  2. 清空模拟器中的app

    1.打开模拟器 2.在左上角得下拉菜单选择“还原内容和设置” 3.选择“还原” ,确定 就ok了! 图解如下:

  3. python采用sqlachmy购物商城

    一.流程图: 二.目录结构: C:\USERS\DAISY\PYCHARMPROJECTS\S12\MARKET │ __init__.py │ __init__.pyc │ ├─backend │ ...

  4. 第十章 设计用户界面 之 构建UI布局

    1. 概述 本章内容包括:实现可在不同区域重用的片段.使用Razor模板设计和实现页面.设计可视结构的布局.基于模板页开发. 2. 主要内容 2.1 实现可在不同区域重用的片段 最简单的重用方式就是在 ...

  5. 常用验证函数isset()/empty()/is_numeric()函数

    1) isset()用来检查变量是否设置,若变量存在且值不为NULL时为TRUE: 检查多个变量时变量要全部存在且值不为NULL时为TRUE: 若用函数unset()释放后再用isset()检测时为F ...

  6. Web 前端开发代码规范(基础)

    一. 引言 对于一个多人团队来说,制定一个统一的规范是必要的,因为个性化的东西无法产生良好的聚合效果,规范化可以提高编码工作效率,使代码保持统一的风格,以便于代码整合和后期维护. 二. HTML/CS ...

  7. android布局带参返回

    package com.lxj.lesson2_3ID19; import com.example.lesson2_3_id19.R; import com.lxj.other.AgeActivity ...

  8. uvm_env——UVM大环境(UVM Environment )

    1 What is uvm_env? uvm_env is used to create and connect the uvm_components like driver, monitors , ...

  9. SIT&UAT

  10. LR中测试dubbo接口的脚本

    import lrapi.lr;import com.alibaba.dubbo.config.ApplicationConfig;import com.alibaba.dubbo.config.Re ...