下面是我用来抓取一个域的所有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()实例。在
相关问题 更多 >
编程相关推荐