一、介绍

    本例子用scrapy-splash爬取电视之家(http://www.tvhome.com/news/)网站的资讯信息,输入给定关键字抓取微信资讯信息。

    给定关键字:数字;融合;电视

    抓取信息内如下:

      1、资讯标题

      2、资讯链接

      3、资讯时间

      4、资讯来源

  二、网站信息

    

    

    

   

    

      

  三、数据抓取

    针对上面的网站信息,来进行抓取

    1、首先抓取信息列表

      抓取代码:sels = site.xpath('//div[@class="main_left fl"]/div[2]/ul/li')

    2、抓取标题

      抓取代码:title = str(sel.xpath('.//h2/a/text()')[0].extract())

    3、抓取链接

      抓取代码:url = str(sel.xpath('.//a/@href')[0].extract())

    4、抓取日期

      抓取代码:strdates = sel.xpath('.//em[@class="em_3"]/text()')

    5、抓取来源

      抓取代码:sources = site.xpath('//span[@class="aside01"]/text()')

  

  

  四、完整代码

  

# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
from scrapy.spiders import Spider
from scrapy_splash import SplashRequest
from scrapy_splash import SplashMiddleware
from scrapy.http import Request, HtmlResponse
from scrapy.selector import Selector
from scrapy_splash import SplashRequest
from scrapy_ott.items import SplashTestItem
from scrapy_ott.mongoDB import mongoDbBase
import scrapy_ott.IniFile
import sys
import os
import re
import time reload(sys)
sys.setdefaultencoding('utf-8') # sys.stdout = open('output.txt', 'w') class tvhomeSpider(Spider):
name = 'tvhome'
db = mongoDbBase() configfile = os.path.join(os.getcwd(), 'scrapy_ott\setting.conf')
cf = scrapy_ott.IniFile.ConfigFile(configfile)
information_wordlist = cf.GetValue("tvhome", "information_keywords").split(';')
websearchurl_list = cf.GetValue("tvhome", "websearchurl").split(';')
start_urls = []
for url in websearchurl_list:
start_urls.append(url) # request需要封装成SplashRequest
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url
, self.parse
, args={'wait': ''}
)
def Comapre_to_days(self,leftdate, rightdate):
'''
比较连个字符串日期,左边日期大于右边日期多少天
:param leftdate: 格式:2017-04-15
:param rightdate: 格式:2017-04-15
:return: 天数
'''
l_time = time.mktime(time.strptime(leftdate, '%Y-%m-%d'))
r_time = time.mktime(time.strptime(rightdate, '%Y-%m-%d'))
result = int(l_time - r_time) / 86400
return result def date_isValid(self, strDateText):
'''
判断日期时间字符串是否合法:如果给定时间大于当前时间是合法,或者说当前时间给定的范围内
:param strDateText: 四种格式 '2小时前'; '2天前' ; '昨天' ;'2017.2.12 '
:return: True:合法;False:不合法
'''
currentDate = time.strftime('%Y-%m-%d')
datePattern = re.compile(r'\d{4}-\d{2}-\d{2}')
strDate = re.findall(datePattern, strDateText)
if len(strDate) == 1:
if self.Comapre_to_days(currentDate, strDate[0]) ==0:
return True, strDate[0]
return False, '' def parse(self, response):
site = Selector(response)
sels = site.xpath('//div[@class="main_left fl"]/div[2]/ul/li') for sel in sels:
strdates = sel.xpath('.//em[@class="em_3"]/text()')
if len(strdates)>0:
flag, date = self.date_isValid(str(strdates[0].extract()))
if flag:
title = str(sel.xpath('.//h2/a/text()')[0].extract())
for keyword in self.information_wordlist:
if title.find(keyword) > -1:
url = str(sel.xpath('.//a/@href')[0].extract())
yield SplashRequest(url
, self.parse_item
, args={'wait': ''},
meta={'date': date, 'url': url,
'keyword': keyword, 'title': title}
) def parse_item(self, response):
site = Selector(response)
it = SplashTestItem()
it['title'] = response.meta['title']
it['url'] = response.meta['url']
it['date'] = response.meta['date']
it['keyword'] = response.meta['keyword']
sources = site.xpath('//span[@class="aside01"]/text()')
if len(sources)>0:
it['source'] = sources[0].extract() # self.db.SaveInformation(it)
return it

scrapy-splash抓取动态数据例子十五的更多相关文章

  1. scrapy-splash抓取动态数据例子十六

    一.介绍 本例子用scrapy-splash爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电 ...

  2. scrapy-splash抓取动态数据例子十四

    一.介绍 本例子用scrapy-splash爬取超级TV网站的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4. ...

  3. scrapy-splash抓取动态数据例子十二

    一.介绍 本例子用scrapy-splash通过搜狗搜索引擎,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二. ...

  4. scrapy-splash抓取动态数据例子十

    一.介绍 本例子用scrapy-splash抓取活动行网站给定关键字抓取活动信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...

  5. scrapy-splash抓取动态数据例子一

    目前,为了加速页面的加载速度,页面的很多部分都是用JS生成的,而对于用scrapy爬虫来说就是一个很大的问题,因为scrapy没有JS engine,所以爬取的都是静态页面,对于JS生成的动态页面都无 ...

  6. scrapy-splash抓取动态数据例子八

    一.介绍 本例子用scrapy-splash抓取界面网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...

  7. scrapy-splash抓取动态数据例子七

    一.介绍 本例子用scrapy-splash抓取36氪网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...

  8. scrapy-splash抓取动态数据例子六

    一.介绍 本例子用scrapy-splash抓取中广互联网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...

  9. scrapy-splash抓取动态数据例子五

    一.介绍 本例子用scrapy-splash抓取智能电视网网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站 ...

随机推荐

  1. Type of flip id

    http://www.haskell.org/pipermail/beginners/2011-March/006477.html The point is that the type of id h ...

  2. 千万不要使用xfce和KDE版Manjaro Linux--之荒谬言论

    Manjaro Linux 使用经验: ①千万不要使用xfce版,虽然性能上廉价,但是吃亏,调声音80%几率卡死调不了,托盘图标很容易不响应!关机的Beep声,分分钟吓死人!按照网上各种方法弄,下次开 ...

  3. doxygen使用

    前言 下面主要讲解linux下Doxygen命令行实现html文档生成的操作,当然也有界面版本操作方式,linux下安装doxygen-gui即可通过doxywizard开启界面操作,windows下 ...

  4. Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向”

    Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向” 原因还未知..

  5. request模拟知乎登录(无验证码机制)

    import request try: import cookielib #python2版本 except: import http.cookiejar as cookielib #python3版 ...

  6. selenium 消息框元素定位处理

    以下内容来自于“风少”的博客 <元素定位:selenium消息框处理 (alert.confirm.prompt)> 基础普及 alert对话框 .细分三种,Alert,prompt,co ...

  7. 区块链开发(五)git、truffle安装

    truffle是以太坊最受欢迎的一个开发框架,本篇博客介绍truffle的下载安装过程. git安装 在安装truffle之前需要核实一下本机是否安装git程序.后面的程序安装需要依赖git. 输入以 ...

  8. 利其器之webstorm快捷键

    总结几个webstorm常用的快捷键(macbook下) 最实用: command + option + 左/右箭头           定位到历史记录中上次/下次编辑的位置 command + b ...

  9. C# T4使用

    最近升级我们的框架到微服务了,而且是dotnetcore 2.0. 然后一个新的框架,最基本的Model和与数据库交互的Repository,我们都是要利用T4自动生成的. 首先这个是代码结构,在这个 ...

  10. 备份文件的python脚本(转)

    作用:将目录备份到其他路径.实际效果:假设给定目录"/media/data/programmer/project/python" ,备份路径"/home/diegoyun ...