首页 > 解决方案 > 使用 scrapy 框架在网站中查找前 5 个常用词

问题描述

我可以通过获取页​​面内容和查找文本表单页面来找出前 5 个常用词而无需刮擦。然后用出现次数将单词转储到字典中。

但我想利用scrapy的设施来做到这一点。但我不确定我应该在项目中的哪里保存该字典以保存字数,以便蜘蛛可以将数据发送到公共位置然后更新该字典。

如何使用 scrapy 查找最常用的单词?
我可以使用scrapy的统计收集模块,以便在完成爬行后我可以打印统计信息吗?

标签: pythonscrapyweb-crawler

解决方案


我以前从未使用过scrapy,但我认为我有一个解决方案来计算HTML正文中的所有单词。

在一个名为的文件words_spider.py中放入以下代码:

from collections import Counter

import scrapy

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://quotes.toscrape.com/tag/humor/',
    ]

    def parse(self, response):
        for text in response.xpath('//body//*//text()').extract():
            # Eliminate empty strings
            words_ = (item.strip() for item in text.strip().split(' '))
            words = [item for item in words_ if item]
            if any(words):
                yield Counter(words)

然后在另一个名为的文件中scrapy_runner.py放入以下代码:

import os
import subprocess
import shlex
import json
from functools import reduce
from operator import add
from collections import Counter
from pprint import pprint

FILENAME = 'counters.json'
SCRIPTNAME = 'words_spider.py'

try:
    os.remove(FILENAME)
except FileNotFoundError:
    pass # No file to remove
# Run the spider.
subprocess.check_call(shlex.split(f'scrapy runspider {SCRIPTNAME} -o {FILENAME}'))

with open(FILENAME) as fh:
    # Create counters out of saved JSON file.
    counts = (Counter(item) for item in json.load(fh))

# Add all the counters together.
pprint(reduce(add, counts), indent=4)

运行脚本python scrapy_runner.py::

输出是:

Counter({ 'humor': 12, 'by': 11, '(about)': 10, 'Tags:': 10, 'a': 7, 'you': 7, 'to': 6, 'in ': 6, 'and': 6, 'is': 5, 'think': 5, 'the': 4, '“the': 3, 'be': 3, 'must': 3, 'can' : 3, 'Quotes': 2, 'it': 2, 'or': 2, 'who': 2, 'books': 2, 'simile': 2, 'thinks': 2, 'sitting': 2 , 'make': 2, '那个': 2,'of': 2, 'beholder': 2, 'time': 2, 'chocolate': 2, 'Charles': 2, 'right': 2, 'it.': 2, 'people': 2, 'with': 2, 'only': 2, 'I': 2, 'truth': 2, 'Scrape': 1, 'Login': 1, 'Viewing': 1, 'tag:': 1, '人,':1,'绅士':1,'女士,':1,'有':1,'不':1,'高兴':1,'好':1,'小说,':1, '无法忍受':1,“愚蠢”:1,“简”:1,“奥斯汀”:1,“素养”:1,“经典”:1,“A”:1,“天”:1,“没有” : 1, '阳光': 1, '喜欢': 1, '知道': 1, '夜晚。': 1, '史蒂夫': 1, '马丁': 1, '明显': 1, ' “任何人”:1,“教堂”:1,“基督徒”:1,“也”:1,“车库”:1,“汽车”:1,“驻军”:1,“凯勒”:1, “宗教”:1,'“美女”:1,“眼睛”:1,“可能”:1,“必要”:1,“来自”:1,“给予”:1,“愚蠢”:1,“误传”:1,' black': 1, 'eye.': 1, 'Jim': 1, 'Henson': 1, ''All': 1, 'need': 1, 'love.': 1, 'But': 1 , 'little': 1, 'now': 1, 'then': 1, "doesn't": 1, 'hurt.': 1, 'M.': 1, 'Schulz': 1, 'food ':1,'“记住,':1、“我们是”:1、“疯狂”:1、“爱”:1、“所以”:1、“它是”:1、“全部”:1、“亲吻”:1、“我” :1,“任何时候”:1,“感觉”:1,“喜欢”:1,“苏珊娜”:1,“柯林斯”:1,“一些”:1,“从不”:1,“去”: 1,“疯狂”:1,“什么”:1,“真实”:1,“可怕”:1,“生活”:1,“他们”:1,“铅”:1,“布考斯基” : 1, '麻烦': 1, '有': 1, 'an': 1, 'open': 1, 'mind,': 1, 'course,': 1, 'will': 1, '坚持': 1, 'on' : 1, 'coming': 1, 'along': 1, 'trying': 1, 'put': 1, 'things': 1, 'Terry': 1, 'Pratchett': 1, 'open-mind' : 1, 'thinking': 1, ''Think': 1, 'left': 1, 'low': 1, 'high.': 1, 'Oh,': 1, 'up': 1, 'if ': 1, '试试吧!'': 1,“博士”:1,“苏斯”:1,“哲学”:1,“原因”:1,“谈话”:1,“我自己”:1,“因为”:1,“我”:1 , 'one': 1, 'whose': 1, 'answers': 1, 'accept.'': 1, 'George': 1, 'Carlin': 1, 'insanity': 1, '谎言': 1 , '说谎': 1, '自我放纵': 1, '下一个': 1, '→': 1, 'Top': 1, '十': 1, '标签': 1, '爱': 1 , '励志':1,'生活':1,'阅读':1,'友谊':1,'朋友':1,'by:':1,'GoodReads.com':1,'Made':1, '❤': 1, 'Scrapinghub': 1})


推荐阅读