scrapy-splash抓取动态数据例子十三
一、介绍
本例子用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抓取动态数据例子十三的更多相关文章
- 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.资讯来源 二.网站 ...
- scrapy-splash抓取动态数据例子四
一.介绍 本例子用scrapy-splash抓取微众圈网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- scrapy-splash抓取动态数据例子三
一.介绍 本例子用scrapy-splash抓取今日头条网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- scrapy-splash抓取动态数据例子二
一.介绍 本例子用scrapy-splash抓取一点资讯网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- scrapy-splash抓取动态数据例子十六
一.介绍 本例子用scrapy-splash爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电 ...
随机推荐
- 基于x64的处理器意思
基于x64的处理器意思是CPU的架构是X64的,也是64位的CPU. 基本简介: "x86-64",有时会简称为"x64",是64位微处理器架构及其相应指令集的 ...
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- 【反演复习计划】【bzoj3994】DZY loves maths
这题大概就是提取一下d,然后就跟前面的题目差不多了. #include<bits/stdc++.h> #define N 10000005 using namespace std; typ ...
- cpu事实负载使用top命令
参考网址:http://www.cnblogs.com/tippoint/archive/2013/03/05/2944319.html 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据 ...
- Selenium2+python自动化60-异常后截图(screenshot)【转载】
前言 在执行用例过程中由于是无人值守的,用例运行报错的时候,我们希望能对当前屏幕截图,留下证据. 在写用例的时候,最后一步是断言,可以把截图的动作放在断言这里,那么如何在断言失败后截图呢? 一.截图方 ...
- 【hdoj_1051】WoodenSticks
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1051 题意可以理解为:给定若干个二元数对,要将这些数对分为不同的组,同一组中的若干个二元数对可以排列成一个 ...
- 安装Team Services Agent Win7
现状:项目现时使用的是Team Services,使用Team Services可以控制其中的一台Build Server,从Github提取代码,并在Build Server进入编译打包处理(son ...
- validate+jquery+ajax表单验证
1.案例 1.1 Html form表单内容 <form class="cForm" id="cForm" method="post" ...
- Eureka Server设计(转载 石杉的架构笔记)
目录: 一.问题起源 二.Eureka Server设计精妙的注册表存储结构 三.Eureka Server端优秀的多级缓存机制 四.总结 一.问题起源 Spring Cloud架构体系中,Eurek ...
- 容斥原理 求M以内有多少个跟N是互质的
开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理! 本题是求[a,b]中与n ...