1. 引言
利用scrapy框架爬取微博熱搜榜網(wǎng)站前50條熱搜。
爬取信息:熱搜排名、熱搜新聞名、熱搜新聞熱搜量。
數(shù)據(jù)存儲(chǔ):存儲(chǔ)為.csv文件。
2.爬取流程
- 新建scrapy爬蟲項(xiàng)目:
在終端輸入以下代碼,創(chuàng)建一個(gè)基于scrapy框架的爬蟲項(xiàng)目,該項(xiàng)目為:weiboreshou。
scrapy startproject weiboreshou
終端
- 在weiboreshou項(xiàng)目下新建爬蟲程序文件
在終端輸入以下代碼,創(chuàng)建一個(gè)名為reshou的爬蟲程序文件。
cd weigoreshou
scrapy genspider reshou s.weibo.com
在這里插入圖片描述
新建的項(xiàng)目包含以下文件:
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
- 編寫items.py文件,定義需要獲取的信息字段。
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import scrapy
class WeiboreshouItem(scrapy.Item):
order = scrapy.Field() #熱搜排名
news = scrapy.Field() #熱搜新聞
number = scrapy.Field() #熱搜量
# define the fields for your item here like:
# name = scrapy.Field()
#pass
- 編寫settings.py程序,進(jìn)行項(xiàng)目的一些設(shè)置
# Scrapy settings for weiboreshou 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 = 'weiboreshou'
SPIDER_MODULES = ['weiboreshou.spiders']
NEWSPIDER_MODULE = 'weiboreshou.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'weiboreshou (+http://www.yourdomain.com)'
# Obey robots.txt rules
ROBOTSTXT_OBEY = False #設(shè)置為不遵守ROBOTSTXT協(xié)議
# 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 = 3 #設(shè)置下載延遲時(shí)間為3秒
# 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.73'
}
# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'weiboreshou.middlewares.WeiboreshouSpiderMiddleware': 543,
#}
# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'weiboreshou.middlewares.WeiboreshouDownloaderMiddleware': 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 = {
# 'weiboreshou.pipelines.WeiboreshouPipeline': 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'
-
編寫reshou.py文件,爬取網(wǎng)站信息
利用CSS選擇器定位所需信息的位置,解析HTML源碼。
在這里插入圖片描述
import scrapy
from ..items import WeiboreshouItem
class ReshouSpider(scrapy.Spider):
name = 'reshou'
allowed_domains = ['s.weibo.com']
start_urls = ['https://s.weibo.com/top/summary']
def parse(self, response):
list_selector = response.css('tbody tr')
for one_selector in list_selector:
order = one_selector.css(' td.ranktop::text').extract()
news = one_selector.css(' a::text').extract()
number = one_selector.css(' span::text').extract()
item = WeiboreshouItem()
item['order'] = order
item['news'] = news
item['number'] = number
yield item
#pass
- 在終端輸入以下程序,進(jìn)行數(shù)據(jù)爬取
scrapy crawl reshou -o reshou.csv
3.查看輸出數(shù)據(jù)
吳亦凡被批捕
寫在最后
關(guān)于scrapy框架,大家可以參考本專欄中的《功能強(qiáng)大的python包(八):Scrapy(網(wǎng)絡(luò)爬蟲)》