Im wondering what would be the most eficient way in order to find if a text that has been scraped using Scrapy contains a word that is in a predefined list. Important to note that the list could be of around ~200 words and the text could be from hundreds of websites so efficiency is important.
My current solution with only a couple of words in list would be:
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class BookSpider(CrawlSpider):
name = 'book'
allowed_domains = ['books.toscrape.com']
start_urls = ['http://books.toscrape.com']
rules = (
Rule(LinkExtractor(), callback='parse', follow=True),
)
def parse(self, response):
restricted = ['word', 'word1', 'word2']
text = response.xpath("//body//text()").getall()
for words in restricted:
if words in text:
print('Found a restricted word!')
else:
print('All good!')
What do you think of such a solution? Maybe there is a more efficient way of achievieng the goal?