Python爬取贴吧中的图片
#看到贴吧大佬在发图,准备盗一下
#只是爬取一个帖子中的图片
1、先新建一个scrapy项目
scrapy startproject TuBaEx
2、新建一个爬虫
scrapy genspider tubaex https://tieba.baidu.com/p/4092816277
3、先写下items
#保存图片的url
img_url=scrapy.Field()
4、开始写爬虫
# -*- coding: utf-8 -*-
import scrapy
from TuBaEx.items import TubaexItem class TubaexSpider(scrapy.Spider):
name = "tubaex"
#allowed_domains = ["https://tieba.baidu.com/p/4092816277"]
baseURL="https://tieba.baidu.com/p/4092816277?pn=" #拼接地址用 实现翻页
offset=0
#要爬取的网页
start_urls = [baseURL+str(offset)] def parse(self, response): #获取最后一页的数字
end_page=response.xpath("//div[@id='thread_theme_5']/div/ul/li[2]/span[2]/text()").extract()
#通过审查元素找到图片的类名,用xpath获取
img_list=response.xpath("//img[@class='BDE_Image']/@src").extract() for img in img_list:
item=TubaexItem()
item['img_url']=img
yield item url=self.baseURL #进行翻页
if self.offset < int(end_page[0]): #通过xpath返回的是list
self.offset+=1
yield scrapy.Request(self.baseURL+str(self.offset),callback=self.parse)
5、使用ImagesPipeline,这个没什么说的,我也不太懂
# -*- coding: utf-8 -*- import requests
from scrapy.pipelines.images import ImagesPipeline
from TuBaEx import settings class TubaexPipeline(ImagesPipeline): def get_media_requests(self,item,info):
img_link = item['img_url']
yield scrapy.Request(img_link) def item_completed(self,results,item,info):
images_store="C:/Users/ll/Desktop/py/TuBaEx/Images/"
img_path=item['img_url']
return item
6、配置下settings
IMAGES_STORE = 'C:/Users/ll/Desktop/py/TuBaEx/Images/'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'TuBaEx (+http://www.yourdomain.com)'
USER_AGENT="User-Agent,Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
#开启管道
ITEM_PIPELINES = {
'TuBaEx.pipelines.TubaexPipeline': 300,
}
7、执行
scrapy crawl tubaex
8、收获果实

Python爬取贴吧中的图片的更多相关文章
- Python 爬取煎蛋网妹子图片
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...
- python爬取某站上海租房图片
前言 对于一个net开发这爬虫真真的以前没有写过.这段时间开始学习python爬虫,今天周末无聊写了一段代码爬取上海租房图片,其实很简短就是利用爬虫的第三方库Requests与BeautifulSou ...
- 利用python爬取王者荣耀英雄皮肤图片
前两天看到同学用python爬下来LOL的皮肤图片,感觉挺有趣的,我也想试试,于是决定来爬一爬王者荣耀的英雄和皮肤图片. 首先,我们找到王者的官网http://pvp.qq.com/web201605 ...
- python爬取返利网中值得买中的数据
先使用以前的方法将返利网的数据爬取下来,scrapy框架还不熟练,明日再战scrapy 查找目标数据使用的是beautifulsoup模块. 1.观察网页,寻找规律 打开值得买这块内容 1>分析 ...
- python爬取365好书中小说
需要转载的小伙伴转载后请注明转载的地址 需要用到的库 from bs4 import BeautifulSoup import requests import time 365好书链接:http:// ...
- python爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重
from bs4 import BeautifulSoupimport sys,os,requests,pymongo,timefrom lxml import etreedef get_fenlei ...
- python爬取站长之家植物图片
from lxml import etree from urllib import request import urllib.parse import time import os def hand ...
- 用python爬取全网妹子图片【附源码笔记】
这是晚上没事无聊写的python爬虫小程序,专门爬取妹子图的,养眼用的,嘻嘻!身为程序狗只会这个了! 废话不多说,代码附上,仅供参考学习! """ 功能:爬取妹子图全网妹 ...
- 使用python爬取P站图片
刚开学时有一段时间周末没事,于是经常在P站的特辑里收图,但是P站加载图片的速度比较感人,觉得自己身为计算机专业,怎么可以做一张张图慢慢下这么low的事,而且这样效率的确也太低了,于是就想写个程序来帮我 ...
随机推荐
- 高数A(下)第九章
第九章 空间解析几何 9.2 9.3 9.4 自测题
- 中文命名之Hibernate 5演示 - 使用注解(annotation)而非xml定义映射
前文中文编程:中文命名之Hibernate 4+MySQL演示最后留下了个Hibernate 5之后出现的问题, 于是在Hibernate社区提交了报告: Seemingly regression s ...
- Ubuntu 16.04下Markdown编辑器Haroopad
1.下载deb包 地址:https://bitbucket.org/rhiokim/haroopad-download/downloads/haroopad-v0.13.2-x64.deb 这里是历史 ...
- OSX: node中安装zeromq
1. brew install pkg-config2. brew install zmq3. export PKG_CONFIG_PATH="/usr/local/lib/pkgconfi ...
- 符号变换引擎(Symbol Transform Engine - STE)
在写编译器的过程中.我意识到编译事实上是一种符号变换,比方C语言编译成机器码,事实上是C源代码文件里的符号变换成EXE的16进制符号,这和中文翻译成英语的语言翻译器没什么差别. 每一个程序猿都有自己喜 ...
- I/O虚拟化
note:这里主要记录我对IO虚拟化的理解,希望这篇文章对想了解虚拟化IO的同学有点帮助.这是我在看论文[vale,a switched ethernet for virtual machines]的 ...
- Levenberg–Marquardt algorithm
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGFubWVuZ3dlbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- CodeForces 16B Burglar and Matches(贪心)
B. Burglar and Matches time limit per test 0.5 second memory limit per test 64 megabytes input stand ...
- Linux音频驱动-ALSA概述
概述 ALSA(Advanced Linux Sound Architecture)是linux上主流的音频结构,在没有出现ALSA架构之前,一直使用的是OSS(Open Sound System)音 ...
- android电池管理系统
原文:http://www.2cto.com/kf/201408/326462.html 1.概述 随着移动智能设备的快速发屏,电池的续航能力在很大情况下诱导了大众消费者的购买选择,android系统 ...