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



三、数据抓取
针对上面的网站信息,来进行抓取
1、首先抓取信息列表
抓取代码:sels = site.xpath('//div[@class="vrwrap"]')
2、抓取标题
抓取代码:titles = sel.xpath('.//h3[@class="vrTitle"]/a/text()|.//h3[@class="vrTitle"]/a/em/text()')
3、抓取链接
抓取代码:it['url'] = sel.xpath('.//h3[@class="vrTitle"]/a/@href')[0].extract()
4、抓取日期
抓取代码:strdate = sel.xpath('.//p[@class="news-from"]/text()')
5、抓取来源
抓取代码:sources = sel.xpath('.//p[@class="news-from"]/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 sougouSpider(Spider):
name = 'sougou' 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 = cf.GetValue("sougou", "websearchurl")
start_urls = []
for word in information_wordlist:
start_urls.append('http://news.sogou.com/news?page=1&query=' + word)
start_urls.append('http://news.sogou.com/news?page=2&query=' + word)
start_urls.append('http://news.sogou.com/news?page=3&query=' + 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')
source = strDateText.split(' ')[0]
if strDateText.find('分钟前') > 0 :
return True,source,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,source,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,source,currentDate
return False, '','' def parse(self, response):
keyword = response.meta['keyword']
site = Selector(response)
sels = site.xpath('//div[@class="vrwrap"]')
item_list = []
for sel in sels:
strdate = sel.xpath('.//p[@class="news-from"]/text()')
if len(strdate)>0:
flag, source, date = self.date_isValid(strdate[0].extract())
if flag:
titles = sel.xpath('.//h3[@class="vrTitle"]/a/text()|.//h3[@class="vrTitle"]/a/em/text()')
title = ''
for t in titles:
title += str(t.extract())
if title.find(keyword) > -1:
it = SplashTestItem()
it['source'] = source
it['url'] = sel.xpath('.//h3[@class="vrTitle"]/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抓取动态数据例子十二的更多相关文章
- scrapy-splash抓取动态数据例子十六
一.介绍 本例子用scrapy-splash爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电 ...
- scrapy-splash抓取动态数据例子十五
一.介绍 本例子用scrapy-splash爬取电视之家(http://www.tvhome.com/news/)网站的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信 ...
- scrapy-splash抓取动态数据例子十四
一.介绍 本例子用scrapy-splash爬取超级TV网站的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4. ...
- scrapy-splash抓取动态数据例子十
一.介绍 本例子用scrapy-splash抓取活动行网站给定关键字抓取活动信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- scrapy-splash抓取动态数据例子一
目前,为了加速页面的加载速度,页面的很多部分都是用JS生成的,而对于用scrapy爬虫来说就是一个很大的问题,因为scrapy没有JS engine,所以爬取的都是静态页面,对于JS生成的动态页面都无 ...
- scrapy-splash抓取动态数据例子八
一.介绍 本例子用scrapy-splash抓取界面网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- scrapy-splash抓取动态数据例子七
一.介绍 本例子用scrapy-splash抓取36氪网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- scrapy-splash抓取动态数据例子六
一.介绍 本例子用scrapy-splash抓取中广互联网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- scrapy-splash抓取动态数据例子五
一.介绍 本例子用scrapy-splash抓取智能电视网网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站 ...
随机推荐
- CentOS 7 安装python3.6.1
说明:CentOS 7最小化安装,默认Python 2.7 ,开发需要安装python3.6.1 . 1.检查python版本 [root@bogon ~]# python -VPython 2.7. ...
- camera摄像原理之四:曝光和GAMMA【转】
转自:http://blog.csdn.net/ghostyu/article/details/7912880 从最明亮到最黑暗,假设人眼能够看到一定的范围,那么胶片(或CCD 等电子感光器件)所能表 ...
- Linux内核线程之深入浅出【转】
转自:http://blog.csdn.net/yiyeguzhou100/article/details/53126626 [-] 线程和进程的差别 线程的分类 1 内核线程 2 轻 ...
- Oracle expdp
exp 客户端工具expdp 服务端工具 expdp help=y 帮助命令directory 导出目录逻辑名 --查询默认数据 ...
- 配置Anaconda的jupyter notebook默认主页
1. 在Anaconda Prompt里输:jupyter notebook --generate-config 然后找到这个文件:C:\Users\{YOUR NAME}\.jupyter\jupy ...
- Spring MVC基础篇4
Spring MVC操作原生Servlet 对象 Spring MVC 可以操作原生的Servlet API,如下的这些原生API,可以各自 自由混合使用,也可以和其他非原生 参数组合使用 实例代码: ...
- 如何在qt中使用中文输入法
参考: http://blog.csdn.net/u013812682/article/details/52101088 dpkg -L fcitx-frontend-qt5 到qt安装目录里find ...
- 【hdoj_2037】今年暑假不AC
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2037 可以这样理解题意:将每个节目看做是一个区间,起始时间为左右端点,待求的是:最多可以有多少个区间互不相 ...
- CentOS7 中把默认yum源更换成163源
163源是目前国内最好用的源,速度是相当快的,现在我们把CentOS7中的源改为163源 1.进入yum源配置文件 cd /etc/yum.repos.d 2.备份一下当前的源,以防出错后可以还原回来 ...
- 将win平台上的mysql数据复制到linux上报错Can't write; duplicate key in table
将win平台上的mysql数据复制到linux上报错Can't write; duplicate key in table xxx 新年新气象,果然在新年的第一天就遇到了一个大坑,项目在win上跑的没 ...