码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-06-09 16:27:43      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:blog   word   例子   ace   art   ima   yield   大于   out   

一、介绍

    本例子用scrapy-splash抓取界面网站给定关键字抓取咨询信息。

    给定关键字:个性化;融合;电视

    抓取信息内如下:

      1、资讯标题

      2、资讯链接

      3、资讯时间

      4、资讯来源

 

  二、网站信息

    技术分享

 

 

 

    技术分享

 

      技术分享

 

  三、数据抓取

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

    1、首先抓取信息列表

      抓取代码:sels = site.xpath(‘//div[contains(@class,"news-view")]‘)

    2、抓取标题

      抓取代码:title = sel.xpath(‘.//div[@class="news-header"]/h3/a/@title‘)[0].extract()

    3、抓取链接

      抓取代码:it[‘url‘] = sel.xpath(‘.//div[@class="news-header"]/h3/a/@href‘)[0].extract()

    4、抓取日期

      抓取代码:dates = sel.xpath(‘.//div[@class="news-footer"]/p/span[2]/text()‘)

    5、抓取来源

      抓取代码:sources =sel.xpath(‘.//div[@class="news-footer"]/p/span[1]/a/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 splash_test.items import SplashTestItem
import IniFile
import sys
import os
import re
import time

reload(sys)
sys.setdefaultencoding(utf-8)

# sys.stdout = open(‘output.txt‘, ‘w‘)

class jiemianSpider(Spider):
    name = jiemian

    configfile = os.path.join(os.getcwd(), splash_test\spiders\setting.conf)

    cf = IniFile.ConfigFile(configfile)
    information_keywords = cf.GetValue("section", "information_keywords")
    information_wordlist = information_keywords.split(;)
    websearchurl = cf.GetValue("jiemian", "websearchurl")
    start_urls = []
    for word in information_wordlist:
        print websearchurl + word
        start_urls.append(websearchurl + word)

    # request需要封装成SplashRequest
    def start_requests(self):
        for url in self.start_urls:
            index = url.rfind(=)
            yield SplashRequest(url
                                , self.parse
                                , args={wait: 2},
                                meta={keyword: url[index + 1:]}
                                )

    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):

        currentDate = time.strftime(%Y-%m-%d)
        datePattern = re.compile(r\d{4}-\d{1,2}-\d{1,2})
        dt = strDateText.replace(/, -)
        strDate = re.findall(datePattern, dt)
        if len(strDate) == 1:
            if self.Comapre_to_days(currentDate, strDate[0]) == 0:
                return True, currentDate
        return False, ‘‘

    def parse(self, response):
        site = Selector(response)

        sels = site.xpath(//div[contains(@class,"news-view")])
        keyword = response.meta[keyword]
        item_list = []
        for sel in sels:
            dates = sel.xpath(.//div[@class="news-footer"]/p/span[2]/text())
            flag,date =self.date_isValid(dates[0].extract())
            title = sel.xpath(.//div[@class="news-header"]/h3/a/@title)[0].extract()
            if  flag and title.find(keyword)>-1:
                it = SplashTestItem()
                it[title] = title
                it[url] = sel.xpath(.//div[@class="news-header"]/h3/a/@href)[0].extract()
                it[date] = date
                it[keyword] = keyword
                sources =sel.xpath(.//div[@class="news-footer"]/p/span[1]/a/text())
                if len(sources)>0:
                    it[source] = sources[0].extract()
                item_list.append(it)
        return item_list

 

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

标签:blog   word   例子   ace   art   ima   yield   大于   out   

原文地址:http://www.cnblogs.com/shaosks/p/6972494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!