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. struts2与struts1的比较

    struts2相对于struts1来说简单了很多,并且功能强大了很多,我们可以从几个方面来看: 从体系结构来看:struts2大量使用拦截器来出来请求,从而允许与业务逻辑控制器 与 servlet-a ...

  2. Python-4-设置字符串的格式字符串

    字符串是不可变的,所有元素赋值和切片赋值都是非法的   1.替换字段名 可以按顺序和名称匹配 >>> "{foo} {} {bar} {}".format(1, ...

  3. STP-4-每VLAN生成树和Trunk上的STP

    如果在有冗余链路且有多个VLAN的交换网络中只使用 STP实例,那么在稳定状态中,仍会有一些端口处于阻塞状态不被使用,冗余链路实际上变成了备份链路. PVST+特性能为每个VLAN创建一个STP实例. ...

  4. 解决 openSUSE 中 Sublime Text 3 的中文显示和输入问题

    测试环境 系统版本:openSUSE Leap 42.2 桌面环境:KDE Plasma 5输入法:fcitx-rime (中州韵) 见周围用 Windows 和 macOS 的小伙伴几乎都在用简单强 ...

  5. C/S 和 B/S 架构

    浏览器/服务器结构.它是C/S架构的一种改进,可以说属于三层C/S架构. 比较大的差别1.结构 C/S是两层架构,由客户端和服务器组成,而B/S是三层架构,由浏览器,WEB服务器和数据库服务器组成. ...

  6. 简单了解junit的使用

    普通使用: 在没有用测试框架之前,我们要用一个main方法来跑代码,而有了像junit这样的测试框架后,就可以不用次次写个main方法了. 直接上代码,注释里有说明: package com.stuP ...

  7. springmvc当要返回中文字符串时出现乱码

    当过滤器,页面编码都对,tomcat版本在8以上(8内部默认用utf-8) 在方法参数中加上,produces="text/html;charset=UTF-8" 绝对可以解决!! ...

  8. Spring使用_进阶

    概述 本文主要写了几个关于Spring Aware,多线程,计划任务(定时任务),条件注解,组合注解,元注解,Spring测试的小例子以及关于@Enable*注解的工作原理的理解. Spring Aw ...

  9. go实现生产者消费者

    package main import ( "fmt" "math/rand" ) func main() { ch := make(chan int) don ...

  10. FTP上传下载 C#辅助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...