[英]Scrapy Spiders - Handling non-HTML links (PDFs, PPT, etc..)
I'm learning Scrapy and Python, and started from a blank project. 我正在学习Scrapy和Python,并从一个空白项目开始。 i'm using Scrapy LxmlLinkExtractor to parse links but the spider always gets stuck when encountering non-HTML links/pages (like PDfs or other document). 我正在使用Scrapy LxmlLinkExtractor解析链接,但是在遇到非HTML链接/页面(例如PDfs或其他文档)时,蜘蛛始终会卡住。
Question: how do we handle - in general - those links with Scrapy, if i only want to store thoses URls (i don't want the content of the document for now...) 问题:如果我只想存储那些URls,我们通常如何处理与Scrapy的那些链接(我现在暂时不希望文档的内容...)
Example page with the documents: http://afcorfmc.org/2009.html 包含文档的示例页面: http : //afcorfmc.org/2009.html
Here is me spider code : 这是我的蜘蛛代码:
#!/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
As explained in scrapy LinkExtractor docs , LxmlLinkExtractor
excludes links with some extensions by default: see https://github.com/scrapy/scrapy/blob/master/scrapy/linkextractors/ init .py#L20 如scrapy LinkExtractor文档中所述 , LxmlLinkExtractor
默认情况下不包括具有某些扩展名的链接:请参阅https://github.com/scrapy/scrapy/blob/master/scrapy/linkextractors/ init .py#L20
This list of extensions includes .pdf
, .ppt
. 扩展名列表包括.pdf
, .ppt
。
You can add a deny_extensions
parameter to your LxmlLinkExtractor
instance and leave it empty for example: 您可以将deny_extensions
参数添加到LxmlLinkExtractor
实例,并将其留空,例如:
$ scrapy shell http://afcorfmc.org/2009.html
2014-10-27 10:27:02+0100 [scrapy] INFO: Scrapy 0.24.4 started (bot: scrapybot)
...
2014-10-27 10:27:03+0100 [default] DEBUG: Crawled (200) <GET http://afcorfmc.org/2009.html> (referer: None)
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x7f5b1a6f4910>
[s] item {}
[s] request <GET http://afcorfmc.org/2009.html>
[s] response <200 http://afcorfmc.org/2009.html>
[s] settings <scrapy.settings.Settings object at 0x7f5b2013f450>
[s] spider <Spider 'default' at 0x7f5b19e9bed0>
[s] Useful shortcuts:
[s] shelp() Shell help (print this help)
[s] fetch(req_or_url) Fetch request (or URL) and update local objects
[s] view(response) View response in a browser
In [1]: from scrapy.contrib.linkextractors.lxmlhtml import LxmlLinkExtractor
In [2]: lx = LxmlLinkExtractor(allow=(),deny=(),restrict_xpaths=(), deny_extensions=())
In [3]: lx.extract_links(response)
Out[3]:
[Link(url='http://afcorfmc.org/documents/TOPOS/2009/MARS/ANATOMO_PATHOLOGIE_Dr_Guinebretiere.ppt', text='ANATOMO_PATHOLOGIE_Dr_Guinebretiere.ppt', fragment='', nofollow=False),
Link(url='http://afcorfmc.org/documents/TOPOS/2009/MARS/CHIMIOTHERAPIE_Dr_Toledano.ppt', text='CHIMIOTHERAPIE_Dr_Toledano.ppt', fragment='', nofollow=False),
Link(url='http://afcorfmc.org/documents/TOPOS/2009/MARS/CHIRURGIE_Dr_Guglielmina.ppt', text='CHIRURGIE_Dr_Guglielmina.ppt', fragment='', nofollow=False),
Link(url='http://afcorfmc.org/documents/TOPOS/2009/MARS/CHIRURGIE_Dr_Sebban.ppt', text='CHIRURGIE_Dr_Sebban.ppt', fragment='', nofollow=False),
Link(url='http://afcorfmc.org/documents/TOPOS/2009/MARS/Cas_clinique_oesophage.ppt', text='Cas_clinique_oesophage.ppt', fragment='', nofollow=False),
Link(url='http://afcorfmc.org/documents/TOPOS/2009/MARS/IMAGERIE_Dr_Seror.ppt', text='IMAGERIE_Dr_Seror.ppt', fragment='', nofollow=False),
...
Link(url='http://afcorfmc.org/documents/TOPOS/2009/OCTOBRE/VB4_Technique%20monoisocentrique%20dans%20le%20sein%20Vero%20Avignon%202009.pdf', text='VB4_Technique monoisocentrique dans le sein Vero Avignon 2009.pdf', fragment='', nofollow=False)]
In [4]:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.