我正在学习 Scrapy 和 Python,并从一个空白项目开始。我正在使用 Scrapy LxmlLinkExtractor 解析链接,但蜘蛛在遇到非 HTML 链接/页面(如 PDfs 或其他文档)时总是卡住。
问题:我们如何处理 - 一般来说 - 那些带有 Scrapy 的链接,如果我只想存储那些 URls(我现在不想要文档的内容......)
包含文档的示例页面:http: //afcorfmc.org/2009.html
这是我的蜘蛛代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.lxmlhtml import LxmlLinkExtractor
from super.items import SuperItem
from scrapy.selector import Selector
class mySuper(CrawlSpider):
name="super"
#on autorise seulement le crawl du site indiqué dans allowed_domains
allowed_domains = ['afcorfmc.org']
#démarrage sur la page d'accueil du site
start_urls = ['http://afcorfmc.org']
rules = (Rule (LxmlLinkExtractor(allow=(),deny=(),restrict_xpaths=()), callback="parse_o", follow= True),)
def parse_o(self,response):
#récupération des datas récoltées (contenu de la page)
sel = Selector(response)
#on prépare item, on va le remplir (souvenez-vous, dans items.py)
item = SuperItem()
#on stocke l'url de la page dans le tableau item
item['url'] = response.url
#on récupère le titre de la page ( titre ) grâce à un chemin xpath
#item['titre'] = sel.xpath('//title/text()').extract()
# on fait passer item à la suite du processus
yield item