#看到贴吧大佬在发图,准备盗一下

#只是爬取一个帖子中的图片

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爬取贴吧中的图片的更多相关文章

  1. Python 爬取煎蛋网妹子图片

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...

  2. python爬取某站上海租房图片

    前言 对于一个net开发这爬虫真真的以前没有写过.这段时间开始学习python爬虫,今天周末无聊写了一段代码爬取上海租房图片,其实很简短就是利用爬虫的第三方库Requests与BeautifulSou ...

  3. 利用python爬取王者荣耀英雄皮肤图片

    前两天看到同学用python爬下来LOL的皮肤图片,感觉挺有趣的,我也想试试,于是决定来爬一爬王者荣耀的英雄和皮肤图片. 首先,我们找到王者的官网http://pvp.qq.com/web201605 ...

  4. python爬取返利网中值得买中的数据

    先使用以前的方法将返利网的数据爬取下来,scrapy框架还不熟练,明日再战scrapy 查找目标数据使用的是beautifulsoup模块. 1.观察网页,寻找规律 打开值得买这块内容 1>分析 ...

  5. python爬取365好书中小说

    需要转载的小伙伴转载后请注明转载的地址 需要用到的库 from bs4 import BeautifulSoup import requests import time 365好书链接:http:// ...

  6. python爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重

    from bs4 import BeautifulSoupimport sys,os,requests,pymongo,timefrom lxml import etreedef get_fenlei ...

  7. python爬取站长之家植物图片

    from lxml import etree from urllib import request import urllib.parse import time import os def hand ...

  8. 用python爬取全网妹子图片【附源码笔记】

    这是晚上没事无聊写的python爬虫小程序,专门爬取妹子图的,养眼用的,嘻嘻!身为程序狗只会这个了! 废话不多说,代码附上,仅供参考学习! """ 功能:爬取妹子图全网妹 ...

  9. 使用python爬取P站图片

    刚开学时有一段时间周末没事,于是经常在P站的特辑里收图,但是P站加载图片的速度比较感人,觉得自己身为计算机专业,怎么可以做一张张图慢慢下这么low的事,而且这样效率的确也太低了,于是就想写个程序来帮我 ...

随机推荐

  1. 不动点(Fixed Point)

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51735818 在数学中,函数的不动点( ...

  2. Spark在Executor上的内存分配

    spark.serializer (default org.apache.spark.serializer.JavaSerializer )    建议设置为 org.apache.spark.ser ...

  3. vue.js嵌套路由-------由浅入深

    嵌套路由就是路由里面嵌套他的子路由 子路由关键属性children 每一个子路由里面可以嵌套多个组件 子组件又有路由导航和路由容器 <router-link to="/父路由的地址名字 ...

  4. nyoj_278_排队_201403282135

    排队 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 周末了,软件ACM的队员准备玩玩游戏,娱乐一下,CY想了一个好主意,所有队员站成一个圈,从1开始报数,凡是报出指 ...

  5. java包静态导入,继承

    /** * 静态导入 */package cn.sxt.gao;import cn.sxt.oo.*;//用别的包的类需要导入别的包,表示该包下所有类全部导入import java.util.Date ...

  6. mysql中有关树的函数

    用mysql客户端在库中建立函数queryOrgChildren(查找子节点)和queryOrgLevel(查看当前节点在树中的级别):DROP FUNCTION IF EXISTS `queryOr ...

  7. gh-ost: triggerless online schema migrations:Blog by Shlomi Noach:

    http://code.openark.org/blog/category/mysql https://rj03hou.github.io/mysql/gh-ost/

  8. MySQL: Create Master - Slave Servers

    http://www.unixmen.com/setup-mariadb-master-slave-replication-in-centos-7/ and http://www.rackspace. ...

  9. hdu 4587 推断孤立点+割点+ 删除点之后,剩下多少连通分量

    做了非常久...... 题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4587 先枚举删除的第一个点,第二个点就是找割点.没有割点当然也有答案 学到 ...

  10. Unity3d PBR海水渲染

    三层水 博主近期渲染:近期用unity5弄的一些渲染 ---- by wolf96  http://blog.csdn.net/wolf96