一、介绍

    本例子用scrapy-splash通过搜狗搜索引擎,输入给定关键字抓取微信资讯信息。

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

    抓取信息内如下:

      1、资讯标题

      2、资讯链接

      3、资讯时间

      4、资讯来源

  二、网站信息

    

    

      

      

  三、数据抓取

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

    1、首先抓取信息列表

      抓取代码:sels = site.xpath('//li[contains(@id,"sogou_vr")]')

    2、抓取标题

      抓取代码:titles = sel.xpath('.//div[@class="txt-box"]/h3/a/text()|.//div[@class="txt-box"]/h3/a/em/text()')

    3、抓取链接

      抓取代码:sel.xpath('.//div[@class="txt-box"]/h3/a/@href')[0].extract()

    4、抓取日期

      抓取代码:strdate = sel.xpath('.//span[@class="s2"]/text()')

    5、抓取来源

      抓取代码:sources = sel.xpath('.//div[@class="s-p"]/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 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 weixinSpider(Spider):
name = 'weixin' db = mongoDbBase() configfile = os.path.join(os.getcwd(), 'scrapy_ott\setting.conf')
cf = scrapy_ott.IniFile.ConfigFile(configfile)
information_keywords = cf.GetValue("section", "information_keywords")
information_wordlist = information_keywords.split(';')
websearchurl_list = cf.GetValue("weixin", "websearchurl").split(';')
start_urls = []
for word in information_wordlist:
for url in websearchurl_list:
start_urls.append(url + word) # request需要封装成SplashRequest
def start_requests(self): for url in self.start_urls:
index = url.rfind('=')
yield SplashRequest(url
, self.parse
, args={'wait': ''},
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):
'''
判断日期时间字符串是否合法:如果给定时间大于当前时间是合法,或者说当前时间给定的范围内
:param strDateText: 四种格式 '慧聪网 7小时前'; '新浪游戏 29分钟前' ; '中国行业研究网 2017-6-13'
:return: True:合法;False:不合法
'''
currentDate = time.strftime('%Y-%m-%d') if strDateText.find('分钟前') > 0 :
return True,currentDate
elif strDateText.find('小时前') > 0:
datePattern = re.compile(r'\d{1,2}')
ch = int(time.strftime('%H')) # 当前小时数
strDate = re.findall(datePattern, strDateText)
if len(strDate) == 1:
if int(strDate[0]) <= ch: # 只有小于当前小时数,才认为是今天
return True,currentDate
else:
datePattern = re.compile(r'\d{4}-\d{1,2}-\d{1,2}')
strDate = re.findall(datePattern, strDateText)
if len(strDate) == 1:
if self.Comapre_to_days(currentDate, strDate[0]) == 0:
return True,currentDate
return False, '' def parse(self, response):
keyword = response.meta['keyword']
site = Selector(response)
sels = site.xpath('//li[contains(@id,"sogou_vr")]')
item_list = []
for sel in sels:
strdate = sel.xpath('.//span[@class="s2"]/text()')
if len(strdate)>0:
flag,date = self.date_isValid(strdate[0].extract())
if flag:
titles = sel.xpath('.//div[@class="txt-box"]/h3/a/text()|.//div[@class="txt-box"]/h3/a/em/text()')
title = ''
for t in titles:
title += str(t.extract())
if title.find(keyword) > -1:
it = SplashTestItem()
sources = sel.xpath('.//div[@class="s-p"]/a/text()')
if len(sources)>0:
it['source'] = sources[0].extract()
it['url'] = sel.xpath('.//div[@class="txt-box"]/h3/a/@href')[0].extract()
it['date'] = date
it['keyword'] = keyword
it['title'] = title
item_list.append(it) if len(item_list)>0:
# self.db.SaveInformations(item_list)
return item_list

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【转】使用者角度看bionic pthread_mutex和linux futex实现

    使用者角度看bionic pthread_mutex和linux futex实现 本文所大篇幅引用的参考文章主要描述针对glibc和pthread实现:而本文的考察代码主要是android的bioni ...

  2. GitLab版本管理【转】

    转自:http://www.cnblogs.com/wintersun/p/3930900.html GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的Git项目 ...

  3. 查询ubuntu系统版本相关信息

    查询ubuntu系统版本相关信息 sky@sky-virtual-machine:~$ cat /etc/issueUbuntu 12.04.5 LTS \n \l proc目录下记录的当前系统运行的 ...

  4. wscript运行js文件

    wscript运行js文件 http://www.cnblogs.com/jxgxy/archive/2013/09/20/3330818.html wscript运行js文件 wscript  ad ...

  5. unbuntu下mount windows共享目录

    1)sudo apt-get install smbclient 2)sudo mount -t cifs -o username=wcf@fitme.ai,password=Wsy123456 // ...

  6. java连接Fastdfs图片服务器上传失败的解决方法

    照着视频上做,但是却连接不了虚拟机linux上的图片服务器,估计是linux防火墙的问题(这个实在是神烦,前面有好几次连接不了都是因为linux防火墙),果不其然,关闭即可. Linux关闭防火墙的命 ...

  7. hdu 1115(多边形重心问题)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. 详解ListView加载网络图片的优化,让你轻松掌握!

    详解ListView加载网络图片的优化,让你轻松掌握! 写博客辛苦了,转载的朋友请标明出处哦,finddreams(http://blog.csdn.net/finddreams/article/de ...

  9. firefox中outlook.com页面卡顿的原因

    在火狐中使用outlook.com时,鼠标点击动作后,页面会卡顿一段时间,每次点击都是如此. 因为之前火狐出现由于硬件加速导致页面卡顿的情况,因此第一反应就是关闭硬件加速. 果然,关闭硬件加速后,页面 ...

  10. CSS3实现鼠标经过图片时图片放大

    在鼠标经过图片时,图片被放大,而且还有个过渡的效果.... <!DOCTYPE html> <html> <head> <link href="cs ...