python 斗图图片爬虫
捣鼓了三小时,有一些小Bug,望大佬指导
废话不说,直接上代码:
#!/usr/bin/python3
# -*- coding:UTF-8 -*-
import os,re,requests
from urllib import request,parse class Doutu_api(object):
def __init__(self):
self.api_html = r'http://www.doutula.com/search?keyword=%s'
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'}
self.path = os.path.dirname(os.path.realpath(__file__))+'\\temp' def make_path(self,path=''):#返回假为已创建,否则创建新文件夹
self.path = self.path+'\\'+path
if os.path.exists(self.path): # 判断文件夹是否存在
return False
else:
os.mkdir(self.path) # 创建文件夹
return True def get_img_html(self,html):
self.make_path(path=html)
html = self.api_html%parse.quote(html)
pattern = re.compile(u'<a.*?class="col-xs-6 col-md-2".*?href="(.*?)".*?style="padding:5px;">.*?</a>',re.S)
pattern_img = re.compile(u'<td>.*?<img.*?src="(.*?)".*?alt="(.*?)".*?onerror=".*?">.*?</td>',re.S)
try:
req = request.Request(html, headers=self.headers)
imgs = request.urlopen(req)
imgs = imgs.read().decode('utf-8')
imgs = re.findall(pattern, imgs)
for img in imgs:
req = request.Request(img, headers=self.headers)
imgurl = request.urlopen(req).read().decode('utf-8')
imgurl =re.findall(pattern_img, imgurl)
with open(self.path+'\\{}.png'.format(imgurl[0][1].replace('/','-')), 'wb') as file:
response = requests.get(imgurl[0][0]).content # 下载图片
file.write(response) # 读取图片
print('已完成下载,图片地址:',self.path)
except Exception as e:
print(e)
return None doutu = Doutu_api()
doutu.get_img_html(input('斗图内容关键字:'))
测试成功


python 斗图图片爬虫的更多相关文章
- py3+requests+urllib+bs4+threading,爬取斗图图片
实现原理及思路请参考我的另外几篇爬虫实践博客 py3+urllib+bs4+反爬,20+行代码教你爬取豆瓣妹子图:http://www.cnblogs.com/UncleYong/p/6892688. ...
- python+tkinter+动画图片+爬虫(查询天气)的GUI图形界面设计
1.完整代码: import time import urllib.request #发送网络请求,获取数据 import gzip #压缩和解压缩模块 import json #解析获得的数据 fr ...
- 【Python】:简单爬虫作业
使用Python编写的图片爬虫作业: #coding=utf-8 import urllib import re def getPage(url): #urllib.urlopen(url[, dat ...
- python多线程爬虫+批量下载斗图啦图片项目(关注、持续更新)
python多线程爬虫项目() 爬取目标:斗图啦(起始url:http://www.doutula.com/photo/list/?page=1) 爬取内容:斗图啦全网图片 使用工具:requests ...
- python爬虫我是斗图之王
python爬虫我是斗图之王 本文会以斗图啦网站为例,爬取所有表情包. 阅读之前需要对线程池.连接池.正则表达式稍作了解. 分析网站 页面url分析 打开斗图啦网站,简单翻阅之后发现最新表情每页包含的 ...
- Python爬虫入门教程 13-100 斗图啦表情包多线程爬取
斗图啦表情包多线程爬取-写在前面 今天在CSDN博客,发现好多人写爬虫都在爬取一个叫做斗图啦的网站,里面很多表情包,然后瞅了瞅,各种实现方式都有,今天我给你实现一个多线程版本的.关键技术点 aioht ...
- 【Python爬虫实战】 图片爬虫-淘宝图片爬虫--千图网图片爬虫
所谓图片爬虫,就是从互联网中自动把对方服务器上的图片爬下来的爬虫程序.有些图片是直接在html文件里面,有些是隐藏在JS文件中,在html文件中只需要我们分析源码就能得到如果是隐藏在JS文件中,那么就 ...
- python 爬虫系列09-异步斗图来一波
斗图斗图,妈妈再也不怕我都不赢了 import requests from lxml import etree from urllib import request import os import ...
- python多线程爬取斗图啦数据
python多线程爬取斗图啦网的表情数据 使用到的技术点 requests请求库 re 正则表达式 pyquery解析库,python实现的jquery threading 线程 queue 队列 ' ...
随机推荐
- 如何设计企业移动应用 by宋凯
移动应用设计内部培训 by宋凯 企业移动应用的特点:简约.效率.增强ERP与环境的结合.及时.安全.企业内社交. 一句话定义你的移动应用:然后围绕这句话来设计你的APP. 一:如何定义你的应用: 1, ...
- Spark Mllib里相似度度量(基于余弦相似度计算不同用户之间相似性)(图文详解)
不多说,直接上干货! 常见的推荐算法 1.基于关系规则的推荐 2.基于内容的推荐 3.人口统计式的推荐 4.协调过滤式的推荐 协调过滤算法,是一种基于群体用户或者物品的典型推荐算法,也是目前常用的推荐 ...
- 性能测试学习第四天_Loadrunner概述
1.Loadrunner的组成部分 ·脚本生成器VuGen:virtual user generator ·压力控制器Controller,License主要在这里控制 ·结果分析软件Analysis ...
- 与 HTML4 比较,HTML5 废弃了哪些元素?
frame frameset noframe applet big center basefront
- 我对USB的认识
一.USB协议规范 (1) 基本概念 每一个设备(device)会有一个或者多个的逻辑连接点在里面,每个连接点叫endpoint.每个endpoint有四种数据传送方式:控制(Contr ...
- weex 项目搭建
第一步:安装依赖 npm install -g weex-toolkit weex -v //查看当前weex版本 weex update weex-devtool@latest //@后标注版本后, ...
- jQuery事件绑定函数:on()与bind()的差别
jQuery从1.7+版本开始,提供了on()和off()进行事件处理函数的绑定和取消.on()和bind()这两个方法有相同的地方也有不同的地方. bind(type,[data],fn); on( ...
- SpringBoot的优缺点
优点: 1.快速构建项目 2.对主流开发框架的无配置继承 3.项目可独立运行,无须外部依赖Servlet容器 4.提高运行时的应用监控 5.极大地提高了开发.部署效率 6.与云计算的天然集成 缺点: ...
- Pixel XL 刷机及Root详细方法
需要准备的文件: 获取 Google USB 驱动程序并安装 https://developer.android.com/studio/run/win-usb 下载Google官方镜像文件 [8.1. ...
- Paoding-Rose学习
* HttpServletRequest.getContextPath 获取web程序root.如果是默认位置,返回””空串,否则返回 /根路径名 * rose是如何扫描到资源的 利用spring提供 ...