I have a problem with a Scrapy Python program I'm trying to build. The code is the following.
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class LinkscrawlItem(scrapy.Item):
link = scrapy.Field()
attr = scrapy.Field()
class someSpider(CrawlSpider):
name = 'mysitecrawler'
item = []
allowed_domains = ['mysite.co.uk']
start_urls = ['https://mysite.co.uk/']
rules = (Rule (LinkExtractor(), callback="parse_obj", follow=True),
Rule (LinkExtractor(deny=('my-account', 'cart', 'checkout', 'wp-content')))
)
def parse_obj(self,response):
item = LinkscrawlItem()
item["link"] = str(response.url)+":"+str(response.status)
filename = 'links2.txt'
with open(filename, 'a') as f:
f.write('\n'+str(response.url)+":"+str(response.status)+'\n')
self.log('Saved file %s' % filename)
I'm having trouble with the LinkExtractor, for me the deny is meant to exclude from the crawl the list of links I gave it. But it is still crawling them. For the first three the URLs are:
https://mysite.co.uk/my-account/
https://mysite.co.uk/checkout/
The last one is containing wp-content, example:
https://mysite.co.uk/wp-content/uploads/01/22/photo.jpg
Would anyone know what I'm doing wrong with my deny list please?
Thank you