日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/7/14 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 timereload(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需要封裝成SplashRequestdef 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) / 86400return resultdef 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,currentDateelif 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,currentDateelse: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,currentDatereturn 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'] = dateit['keyword'] = keywordit['title'] = titleitem_list.append(it)if len(item_list)>0:# self.db.SaveInformations(item_list)return item_list

?

總結

以上是生活随笔為你收集整理的scrapy-splash抓取动态数据例子十三的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。