[英]Scraping a domain for links recursively using Scrapy
這是我用於抓取域中所有URL的代碼:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor
class UrlsSpider(scrapy.Spider):
name = 'urlsspider'
allowed_domains = ['example.com']
start_urls = ['http://example.com/']
rules = (Rule(LxmlLinkExtractor(allow=(), unique=True), callback='parse', follow=True))
def parse(self, response):
for link in LxmlLinkExtractor(allow_domains=self.allowed_domains, unique=True).extract_links(response):
print link.url
yield scrapy.Request(link.url, callback=self.parse)
如您所見,我使用了unique=True
但它仍在終端中打印重復的url,而我只想要唯一的url,而不想要重復的url。
在這方面的任何幫助將非常有幫助。
由於代碼以遞歸方式查看URL的內容,因此您將從其他頁面的解析中看到重復的URL。 本質上,您具有LxmlLinkExtractor()的多個實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.