一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程

Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程

2022-02-27 11:49劍客阿良_ALiang Python

Scrapy是用純Python實現一個為了爬取網站數據、提取結構性數據而編寫的應用框架,用途非常廣泛,框架的力量,用戶只需要定制開發幾個模塊就可以輕松的實現一個爬蟲,用來抓取網頁內容以及各種圖片,非常之方便

前言

接著我的上一篇:Python 詳解爬取并統計CSDN全站熱榜標題關鍵詞詞頻流程

我換成Scrapy架構也實現了一遍。獲取頁面源碼底層原理是一樣的,Scrapy架構更系統一些。下面我會把需要注意的問題,也說明一下。

提供一下GitHub倉庫地址:github本項目地址

 

環境部署

scrapy安裝

pip install scrapy -i https://pypi.douban.com/simple

selenium安裝

pip install selenium -i https://pypi.douban.com/simple

jieba安裝

pip install jieba -i https://pypi.douban.com/simple

IDE:PyCharm

google chrome driver下載對應版本:google chrome driver下載地址

檢查瀏覽器版本,下載對應版本。

Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程

 

實現過程

下面開始搞起。

創建項目

使用scrapy命令創建我們的項目。

scrapy startproject csdn_hot_words

項目結構,如同官方給出的結構。

Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程

定義Item實體

按照之前的邏輯,主要屬性為標題關鍵詞對應出現次數的字典。代碼如下:

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class CsdnHotWordsItem(scrapy.Item):
  # define the fields for your item here like:
  # name = scrapy.Field()
  words = scrapy.Field()

關鍵詞提取工具

使用jieba分詞獲取工具。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/5 23:47
# @Author  : 至尊寶
# @Site    : 
# @File    : analyse_sentence.py

import jieba.analyse


def get_key_word(sentence):
  result_dic = {}
  words_lis = jieba.analyse.extract_tags(
      sentence, topK=3, withWeight=True, allowPOS=())
  for word, flag in words_lis:
      if word in result_dic:
          result_dic[word] += 1
      else:
          result_dic[word] = 1
  return result_dic

爬蟲構造

這里需要給爬蟲初始化一個瀏覽器參數,用來實現頁面的動態加載。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/5 23:47
# @Author  : 至尊寶
# @Site    : 
# @File    : csdn.py

import scrapy
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from csdn_hot_words.items import CsdnHotWordsItem
from csdn_hot_words.tools.analyse_sentence import get_key_word


class CsdnSpider(scrapy.Spider):
  name = 'csdn'
  # allowed_domains = ['blog.csdn.net']
  start_urls = ['https://blog.csdn.net/rank/list']

  def __init__(self):
      chrome_options = Options()
      chrome_options.add_argument('--headless')  # 使用無頭谷歌瀏覽器模式
      chrome_options.add_argument('--disable-gpu')
      chrome_options.add_argument('--no-sandbox')
      self.browser = webdriver.Chrome(chrome_options=chrome_options,
                                      executable_path="E:\\chromedriver_win32\\chromedriver.exe")
      self.browser.set_page_load_timeout(30)

  def parse(self, response, **kwargs):
      titles = response.xpath("//div[@class='hosetitem-title']/a/text()")
      for x in titles:
          item = CsdnHotWordsItem()
          item['words'] = get_key_word(x.get())
          yield item

代碼說明

1、這里使用的是chrome的無頭模式,就不需要有個瀏覽器打開再訪問,都是后臺執行的。

2、需要添加chromedriver的執行文件地址。

3、在parse的部分,可以參考之前我文章的xpath,獲取到標題并且調用關鍵詞提取,構造item對象。

中間件代碼構造

添加js代碼執行內容。中間件完整代碼:

# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals
from scrapy.http import HtmlResponse
from selenium.common.exceptions import TimeoutException
import time

from selenium.webdriver.chrome.options import Options

# useful for handling different item types with a single interface
from itemadapter import is_item, ItemAdapter


class CsdnHotWordsSpiderMiddleware:
  # Not all methods need to be defined. If a method is not defined,
  # scrapy acts as if the spider middleware does not modify the
  # passed objects.

  @classmethod
  def from_crawler(cls, crawler):
      # This method is used by Scrapy to create your spiders.
      s = cls()
      crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
      return s

  def process_spider_input(self, response, spider):
      # Called for each response that goes through the spider
      # middleware and into the spider.

      # Should return None or raise an exception.
      return None

  def process_spider_output(self, response, result, spider):
      # Called with the results returned from the Spider, after
      # it has processed the response.

      # Must return an iterable of Request, or item objects.
      for i in result:
          yield i

  def process_spider_exception(self, response, exception, spider):
      # Called when a spider or process_spider_input() method
      # (from other spider middleware) raises an exception.

      # Should return either None or an iterable of Request or item objects.
      pass

  def process_start_requests(self, start_requests, spider):
      # Called with the start requests of the spider, and works
      # similarly to the process_spider_output() method, except
      # that it doesn't have a response associated.

      # Must return only requests (not items).
      for r in start_requests:
          yield r

  def spider_opened(self, spider):
      spider.logger.info('Spider opened: %s' % spider.name)


class CsdnHotWordsDownloaderMiddleware:
  # Not all methods need to be defined. If a method is not defined,
  # scrapy acts as if the downloader middleware does not modify the
  # passed objects.

  @classmethod
  def from_crawler(cls, crawler):
      # This method is used by Scrapy to create your spiders.
      s = cls()
      crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
      return s

  def process_request(self, request, spider):
      js = '''
                      let height = 0
              let interval = setInterval(() => {
                  window.scrollTo({
                      top: height,
                      behavior: "smooth"
                  });
                  height += 500
              }, 500);
              setTimeout(() => {
                  clearInterval(interval)
              }, 20000);
          '''
      try:
          spider.browser.get(request.url)
          spider.browser.execute_script(js)
          time.sleep(20)
          return HtmlResponse(url=spider.browser.current_url, body=spider.browser.page_source,
                              encoding="utf-8", request=request)
      except TimeoutException as e:
          print('超時異常:{}'.format(e))
          spider.browser.execute_script('window.stop()')
      finally:
          spider.browser.close()

  def process_response(self, request, response, spider):
      # Called with the response returned from the downloader.

      # Must either;
      # - return a Response object
      # - return a Request object
      # - or raise IgnoreRequest
      return response

  def process_exception(self, request, exception, spider):
      # Called when a download handler or a process_request()
      # (from other downloader middleware) raises an exception.

      # Must either:
      # - return None: continue processing this exception
      # - return a Response object: stops process_exception() chain
      # - return a Request object: stops process_exception() chain
      pass

  def spider_opened(self, spider):
      spider.logger.info('Spider opened: %s' % spider.name)

制作自定義pipeline

定義按照詞頻統計最終結果輸出到文件。代碼如下:

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class CsdnHotWordsPipeline:

  def __init__(self):
      self.file = open('result.txt', 'w', encoding='utf-8')
      self.all_words = []

  def process_item(self, item, spider):
      self.all_words.append(item)
      return item

  def close_spider(self, spider):
      key_word_dic = {}
      for y in self.all_words:
          print(y)
          for k, v in y['words'].items():
              if k.lower() in key_word_dic:
                  key_word_dic[k.lower()] += v
              else:
                  key_word_dic[k.lower()] = v
      word_count_sort = sorted(key_word_dic.items(),
                               key=lambda x: x[1], reverse=True)
      for word in word_count_sort:
          self.file.write('{},{}\n'.format(word[0], word[1]))
      self.file.close()

settings配置

配置上要做一些調整。如下調整:

# Scrapy settings for csdn_hot_words project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'csdn_hot_words'

SPIDER_MODULES = ['csdn_hot_words.spiders']
NEWSPIDER_MODULE = 'csdn_hot_words.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'csdn_hot_words (+http://www.yourdomain.com)'
USER_AGENT = 'Mozilla/5.0'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 30
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language': 'en',
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36'
}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
SPIDER_MIDDLEWARES = {
 'csdn_hot_words.middlewares.CsdnHotWordsSpiderMiddleware': 543,
}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
 'csdn_hot_words.middlewares.CsdnHotWordsDownloaderMiddleware': 543,
}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
  'csdn_hot_words.pipelines.CsdnHotWordsPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

執行主程序

可以通過scrapy的命令執行,但是為了看日志方便,加了一個主程序代碼。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/5 22:41
# @Author  : 至尊寶
# @Site    : 
# @File    : main.py
from scrapy import cmdline

cmdline.execute('scrapy crawl csdn'.split())

執行結果

執行部分日志

Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程

得到result.txt結果。

Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程

 

總結

看,java還是yyds。不知道為什么2021這個關鍵詞也可以排名靠前。于是我覺著把我標題也加上2021。

GitHub項目地址在發一遍:github本項目地址

申明一下,本文案例僅研究探索使用,不是為了惡意攻擊。

分享:

凡夫俗子不下苦功夫、死力氣去努力做成一件事,根本就沒資格去談什么天賦不天賦。

――烽火戲諸侯《劍來》

如果本文對你有用的話,請不要吝嗇你的贊,謝謝。

Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程

以上就是Python 詳解通過Scrapy框架實現爬取CSDN全站熱榜標題熱詞流程的詳細內容,更多關于Python Scrapy框架的資料請關注服務器之家其它相關文章!

原文鏈接:https://huyi-aliang.blog.csdn.net/article/details/121177213

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一级精品高清一级毛片 | 好大好硬好深好爽想要吃奶 | 亚洲2017天堂色无码 | 国产情侣视频观看 | 2021国产麻豆剧传媒剧情最新 | 五月天色网站 | 成人在线免费播放 | 亚洲男人网 | 涩涩国产精品福利在线观看 | 四虎最新免费观看网址 | 国产成人精品免费久久久久 | 91人成尤物在线 | 免费观看的毛片 | 国产精品午夜性视频网站 | www.尤物在线 | 关晓彤被调教出奶水 | 欧美日韩看看2015永久免费 | 日韩欧美一区二区在线 | 亚洲网红精品大秀在线观看 | 亚洲第一男人网站 | 国产成人综合亚洲亚洲欧美 | 欧美一级特黄特色大片 | 91香蕉国产在线观看免费永久 | 四虎新网址 | 美女被免费视频 | 日本天堂影院在线播放 | 无人在线观看免费高清视频播放 | 欧美大奶艳星 | 白丝校花掀起短裙呻吟小说 | 成人高清网站 | 国产一精品一av一免费爽爽 | 日本不卡免费新一二三区 | 国产成人免费片在线观看 | 青青草精品在线 | 午夜国产精品 | 久久免费看少妇高潮A片JA | 国产毛片在线观看 | 男人j进女屁股视频在线观看 | 欧美国产精品久久 | 视频一区在线观看 | 五月最新商场女厕所高跟嘘嘘 |