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的事,而且这样效率的确也太低了,于是就想写个程序来帮我 ...
随机推荐
- POJ 2065 高斯消元求解问题
题目大意: f[k] = ∑a[i]*k^i % p 每一个f[k]的值就是字符串上第 k 个元素映射的值,*代表f[k] = 0 , 字母代表f[k] = str[i]-'a'+1 把每一个k^i求 ...
- jquery ajax报Uncaught TypeError :Illegal invocation
使用jquery ajax异步提交的时候报Uncaught TypeError :Illegal invocation错误,报错信息如图: 上网查了一下jquery的这个错误,导致这个错误的原因有俩点 ...
- 自定义EL表达式,将对象转成json格式,关键代码
做javaweb开发的最常用的一个东西el表达式,这个东西是个很好用的东西,但有些时候我们处理复杂的字符串操作,就有些相形见绌了,这个时候就需要用自定义的方法去实现更多简洁方便的事情. 下面自定义一个 ...
- - > 贪心基础入门讲解五——任务执行顺序
分析: 本题可以抽象成,从一个整数开始,每次减去a,再加上b (a,b都是正数),要求每次操作都不产生负数. 针对本题a[i] = R[i], b[i] = R[i] – O[i],注意O[i] &l ...
- 用Docker创建Nexus
步骤如下: 1. 创建持久化目录 $ mkdir /some/dir/nexus-data && chown -R 200 /some/dir/nexus-data 2. 创建镜像并运 ...
- docker: 定时检查docker container的运行状态并发邮件报警
首先创建一个发送邮件的bash脚本 - send_mail.sh: #!/bin/bash curl -s --user 'api:key-xxxxxxxxxxxxx' \ https://api.m ...
- CentOS 7: 设置时区和时间
查看当前时区和时间 $ date $ ls -l /etc/localtime 查看所有可用时区 $ timedatectl list-timezones | grep Asia 设置时区 $ tim ...
- 1.3-动态路由协议RIP①
Dynamic Routing Protocol:动态路由协议 现代IP网络中,主要的动态路由协议: AD/管理距离: 1:DV/距离向量协议:RIP(120)/IGRP(100) 2:LS/链路状态 ...
- “2014年ArcGIS影像高级培训班——5月份北京站”火热报名中!
您从事遥感类的相关工作吗?您正对着一景景影像数据不知从何下手吗?您有TB级甚至更高量级的影像数据须要有效管理.即时分享吗?您须要构建Web端的应用实现影像实时处理.在线分析吗? 您是否已经找到有效的解 ...
- The Breakpoint will not currently be hit. No executable code associated with this line
首先.请确认solutin的属性 C/C++->General-> Debug Information Format 选择Program Database(/Zi) Linking-> ...