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 队列 ' ...
随机推荐
- Asp.NET MVC+WebAPI跨域调用
使用jQuery调用WebApi有时会遇到跨域的问题,今天介绍一种可以简单解决跨域问题的方法. 当我们跨域请求WebAPI的时候会提示以下信息: XMLHttpRequest cannot load ...
- UICollectionView笔记2
WWDC 2012 Session笔记——219 Advanced Collection Views and Building Custom Layouts 这是博主的WWDC2012笔记系列中的一篇 ...
- 韦东山笔记之用busybox构建根文件系统
1 百度搜索busybox进入busybox官网(https://busybox.net/)作者:恒久力行 QQ:624668529 点击左侧DownloadSource下载最新稳定版的busybo ...
- Magento 中一个订单的“生命历程”
当我们在网上愉快的买买买的时候, 你知道在这些屏幕“背后”正在进行着什么吗? 1. 当一个产品被加入到购物车后, 实际上发生了什么? 当第一个产品被加入到购物车, 系统首先会生成一个 quote (q ...
- js 数组操作常用方法
push():在数组后面加入元素,并返回数组的长度: unshift():在数组前面就如元素,并返回数组的长度: pop():删除最后一个元素: var arr =[1,2,3,4,5] ; arr. ...
- C# 只运行一个实例 ShowWindowAsync 窗体隐藏时失效 解决方案
如果窗体已经隐藏,那么利用instance.MainWindowHandle得到的句柄为空,继而ShowWindowAsync 操作失败 不过我们可以使用FindWindow来查找到指定窗体的句柄 只 ...
- thymeleaf 模板语言简介
参考网址: https://blog.csdn.net/mlin_123/article/details/51816533 1.1 Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启 ...
- python基础-数据运算
*按位取反运算规则(按位取反再加1) 详解http://blog.csdn.net/wenxinwukui234/article/details/42119265 详细内容ht ...
- 约瑟夫环问题及PHP代码实现
php实现猴子选大王 <?php /** * @param $n 猴子数量 * @param $m 出列的那个数 */ function king($n,$m){ $monkeys = rang ...
- 为OSSIM添加 ossec的linux agent
1,安装环境 [root@node32 test]# yum groupinstall "Development Tools" -y Installed: byacc.x86_64 ...