我在尝试刮痧的时候遇到了一个奇怪的问题。网址:
为了执行爬行,我设计了这个。
class IkeaSpider(CrawlSpider) :
name = "Ikea"
allower_domains = ["http://www.ikea.com/"]
start_urls = ["http://www.ikea.com/fr/fr/catalog/productsaz/8/"]
rules = (
Rule(SgmlLinkExtractor(allow=[r'.*/catalog/products/\d+']),
callback='parse_page',
follow=True),
)
logging.basicConfig(filename='example.log',level=logging.ERROR)
def parse_page(self, response):
for sel in response.xpath('//div[@class="rightContent"]'):
Blah blah blah
我从命令行启动蜘蛛,我可以看到通常被抓取的URL,但其中一些URL的回调没有工作(大约一半的URL通常被抓取)。
由于这个页面上有150多个链接,这也许可以解释为什么爬虫会缺少回调(作业太多)。你们中的一些人对此有什么想法吗?
这是日志 。
2015-12-25 09:02:55 [scrapy] INFO: Stored csv feed (107 items) in: test.csv2015-12-25 09:02:55 [scrapy] INFO: Dumping Scrapy stats: 'downloaderrequest_bytes':68554,'downloaderrequest_count':217,'downloaderrequest_method_countGET':217,'downloaderresponse_bytes'。4577452, 'downloaderresponse_count': 217, 'downloaderresponse_status_count200': 216, 'downloaderresponse_status_count404': 1, 'dupefilterfiltered': 107, 'file_count': 106, 'file_status_countdownloaded': 106, 'finish_reason': 'finish', 'finish_time': datetime.datetime(2015, 12, 25, 8, 2, 55, 548350), 'item_scraped_count': 107, 'log_countDEBUG': 433, 'log_countERROR': 2, 'log_countINFO': 8, 'log_countWARNING': 1, 'request_depth_max': 2, 'response_received_count': 217, 'schedulequeued': 110, 'schedulequeuedmemory': 110, 'scheduleerenqueued': 110, 'scheduleerenqueuedmemory': 110, 'start_time': datetime.datetime(2015, 12, 25, 8, 2, 28, 656959)2015-12-25 09:02:55 [scrapy] INFO: 蜘蛛关闭(完成
我读了很多关于我的问题的东西,很显然,我的问题是 CrawlSpider
类不够具体。这可能解释了为什么它错过了一些链接,因为一些我无法解释的原因。BaseSpider
类与 start_requests
和 make_requests_from_url
的方法来做更具体的工作。我仍然不完全确定如何精确地做到这一点,这只是一个提示。
我不喜欢这些自动蜘蛛类。我通常只是准确地构建我所需要的东西。
import scrapy
class IkeaSpider(scrapy.Spider) :
name = "Ikea"
allower_domains = ["http://www.ikea.com/"]
start_urls = ["https://www.ikea.com/fr/fr/cat/produits-products/"]
logging.basicConfig(filename='example.log',level=logging.ERROR)
def parse(self, response):
# You could also use a.vn-nav__link::attr(href) selector.
for link in response.css('a:contains("/fr/cat/")::attr(href)').getall()
yield scrapy.Request(link, callback=self.parse_category)
def parse_category(self, response):
# parse items or potential sub categories