Web 如何使用Scrapy抓取网站所有页面上的链接

Web 如何使用Scrapy抓取网站所有页面上的链接,web,web-crawler,scrapy,extract,Web,Web Crawler,Scrapy,Extract,我正在学习scrapy,并试图提取包含以下内容的所有链接:,例如:http://lattes.cnpq.br/0281123427918302 但我不知道网站上包含这些信息的页面是什么。 例如,本网站: http://www.ppgcc.ufv.br/ 我想要的链接在此页面上: http://www.ppgcc.ufv.br/?page_id=697 我能做什么? 我试图使用规则,但我不知道如何正确使用正则表达式。 多谢各位 1编辑---- 我需要在主(ppgcc.ufv.br)站点的所有页

我正在学习scrapy,并试图提取包含以下内容的所有链接:,例如:
http://lattes.cnpq.br/0281123427918302
但我不知道网站上包含这些信息的页面是什么。 例如,本网站:

http://www.ppgcc.ufv.br/
我想要的链接在此页面上:

http://www.ppgcc.ufv.br/?page_id=697
我能做什么? 我试图使用规则,但我不知道如何正确使用正则表达式。 多谢各位

1编辑---- 我需要在主(ppgcc.ufv.br)站点的所有页面上搜索链接类型
(http://lattes.cnpq.br/asequenceofnumber)
。我的目标是获取所有链接lattes.cnpq.br/number,但我不知道它们在哪里。我使用了一个简单的代码,实际上就像:

class ExampleSpider(scrapy.Spider):
    name = "example"
    allowed_domains = ["ppgcc.ufv.br"]
    start_urls = (
        'http://www.ppgcc.ufv.br/',
    )
    rules = [Rule(SgmlLinkExtractor(allow=[r'.*']), follow=True),
             Rule(SgmlLinkExtractor(allow=[r'@href']), callback='parse')]

    def parse(self, response):
        filename = str(random.randint(1, 9999))
        open(filename, 'wb').write(response.body)

#I'm trying to understand how to use rules correctly
2编辑---- 使用:

class ExampleSpider(CrawlSpider):
    name = "example"
    allowed_domains = [".ppgcc.ufv.br"]
    start_urls = (
        'http://www.ppgcc.ufv.br/',
    )
    rules = [Rule(SgmlLinkExtractor(allow=[r'.*']), follow=True),
            Rule(SgmlLinkExtractor(allow=[r'@href']), callback='parse_links')]
    def parse_links(self, response):
        filename = "Lattes.txt"
        arquivo = open(filename, 'wb')
        extractor = LinkExtractor(allow=r'lattes\.cnpq\.br/\d+')
        for link in extractor.extract_links(response):
            url = link.urlextractor = LinkExtractor(allow=r'lattes\.cnpq\.br/\d+')
            arquivo.writelines("%s\n" % url)                
            print url
它告诉我:

C:\Python27\Scripts\tutorial3>scrapy crawl example
2015-06-02 08:08:18-0300 [scrapy] INFO: Scrapy 0.24.6 started (bot: tutorial3)
2015-06-02 08:08:18-0300 [scrapy] INFO: Optional features available: ssl, http11
2015-06-02 08:08:18-0300 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'tutorial3.spiders', 'SPIDER_MODULES': ['tutorial3
.spiders'], 'BOT_NAME': 'tutorial3'}
2015-06-02 08:08:19-0300 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState

2015-06-02 08:08:19-0300 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMidd
leware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMidd
leware, ChunkedTransferMiddleware, DownloaderStats
2015-06-02 08:08:19-0300 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLe
ngthMiddleware, DepthMiddleware
2015-06-02 08:08:19-0300 [scrapy] INFO: Enabled item pipelines:
2015-06-02 08:08:19-0300 [example] INFO: Spider opened
2015-06-02 08:08:19-0300 [example] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-06-02 08:08:19-0300 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-06-02 08:08:19-0300 [scrapy] DEBUG: Web service listening on 127.0.0.1:6080
2015-06-02 08:08:19-0300 [example] DEBUG: Crawled (200) <GET http://www.ppgcc.ufv.br/> (referer: None)
2015-06-02 08:08:19-0300 [example] DEBUG: Filtered offsite request to 'www.cgu.gov.br': <GET http://www.cgu.gov.br/acessoainformacao
gov/>
2015-06-02 08:08:19-0300 [example] DEBUG: Filtered offsite request to 'www.brasil.gov.br': <GET http://www.brasil.gov.br/>
2015-06-02 08:08:19-0300 [example] DEBUG: Filtered offsite request to 'www.ppgcc.ufv.br': <GET http://www.ppgcc.ufv.br/>
2015-06-02 08:08:19-0300 [example] DEBUG: Filtered offsite request to 'www.ufv.br': <GET http://www.ufv.br/>
2015-06-02 08:08:19-0300 [example] DEBUG: Filtered offsite request to 'www.dpi.ufv.br': <GET http://www.dpi.ufv.br/>
2015-06-02 08:08:19-0300 [example] DEBUG: Filtered offsite request to 'www.portal.ufv.br': <GET http://www.portal.ufv.br/?page_id=84
>
2015-06-02 08:08:19-0300 [example] DEBUG: Filtered offsite request to 'www.wordpress.org': <GET http://www.wordpress.org/>
2015-06-02 08:08:19-0300 [example] INFO: Closing spider (finished)
2015-06-02 08:08:19-0300 [example] INFO: Dumping Scrapy stats:
        {'downloader/request_bytes': 215,
         'downloader/request_count': 1,
         'downloader/request_method_count/GET': 1,
         'downloader/response_bytes': 18296,
         'downloader/response_count': 1,
         'downloader/response_status_count/200': 1,
         'finish_reason': 'finished',
         'finish_time': datetime.datetime(2015, 6, 2, 11, 8, 19, 912000),
         'log_count/DEBUG': 10,
         'log_count/INFO': 7,
         'offsite/domains': 7,
         'offsite/filtered': 42,
         'request_depth_max': 1,
         'response_received_count': 1,
         'scheduler/dequeued': 1,
         'scheduler/dequeued/memory': 1,
         'scheduler/enqueued': 1,
         'scheduler/enqueued/memory': 1,
         'start_time': datetime.datetime(2015, 6, 2, 11, 8, 19, 528000)}
2015-06-02 08:08:19-0300 [example] INFO: Spider closed (finished)
C:\Python27\Scripts\tutorial3>scrapy爬网示例
2015-06-02 08:08:18-0300[scrapy]信息:scrapy 0.24.6已启动(机器人程序:教程3)
2015-06-02 08:08:18-0300[scrapy]信息:可选功能可用:ssl、http11
2015-06-02 08:08:18-0300[scrapy]信息:覆盖的设置:{'NEWSPIDER_模块':'tutorial3.SPIDER','SPIDER_模块':['tutorial3
.spider'],'BOT_NAME':'tutorial3'}
2015-06-02 08:08:19-0300[scrapy]信息:启用的扩展:LogStats、TelnetConsole、CloseSpider、WebService、CoreStats、SpiderState
2015-06-02 08:08:19-0300[scrapy]信息:启用的下载程序中间件:HttpAuthMiddleware、DownloadTimeoutMiddleware、UserAgentMidd
leware、RetryMiddleware、DefaultHeadersMiddleware、MetaRefreshMiddleware、HttpCompressionMiddleware、RedirectMiddleware、CookiesMiddle
leware、ChunkedTransfermiddle软件、DownloaderStats
2015-06-02 08:08:19-0300[scrapy]信息:启用的蜘蛛中间件:HttpErrorMiddleware、OffsiteMiddleware、RefererMiddleware、UrlLe
数据中间件,DepthMiddleware
2015-06-02 08:08:19-0300[scrapy]信息:启用的项目管道:
2015-06-02 08:08:19-0300[示例]信息:蜘蛛网已打开
2015-06-02 08:08:19-0300[示例]信息:抓取0页(以0页/分钟的速度),刮取0项(以0项/分钟的速度)
2015-06-02 08:08:19-0300[scrapy]调试:Telnet控制台监听127.0.0.1:6023
2015-06-02 08:08:19-0300[scrapy]调试:在127.0.0.1:6080上侦听Web服务
2015-06-02 08:08:19-0300[示例]调试:爬网(200)(参考:无)
2015-06-02 08:08:19-0300[示例]调试:过滤到“www.cgu.gov.br”的场外请求:
2015-06-02 08:08:19-0300[示例]调试:过滤到“www.brasil.gov.br”的场外请求:
2015-06-02 08:08:19-0300[示例]调试:过滤到“www.ppgcc.ufv.br”的场外请求:
2015-06-02 08:08:19-0300[示例]调试:过滤到“www.ufv.br”的场外请求:
2015-06-02 08:08:19-0300[示例]调试:过滤到“www.dpi.ufv.br”的场外请求:
2015-06-02 08:08:19-0300[示例]调试:过滤到“www.portal.ufv.br”的场外请求:
2015-06-02 08:08:19-0300[示例]调试:过滤到“www.wordpress.org”的场外请求:
2015-06-02 08:08:19-0300[示例]信息:闭合十字轴(已完成)
2015-06-02 08:08:19-0300[示例]信息:倾倒碎屑统计数据:
{'downloader/request_bytes':215,
“下载程序/请求计数”:1,
“downloader/request\u method\u count/GET”:1,
“downloader/response_字节”:18296,
“下载程序/响应计数”:1,
“下载程序/响应状态\计数/200”:1,
“完成原因”:“完成”,
“完成时间”:datetime.datetime(2015,6,2,11,8,19,912000),
“日志计数/调试”:10,
“日志计数/信息”:7,
“异地/域”:7,
“场外/过滤”:42,
“请求深度最大值”:1,
“响应\u已接收\u计数”:1,
“调度程序/出列”:1,
“调度程序/出列/内存”:1,
“调度程序/排队”:1,
“调度程序/排队/内存”:1,
“开始时间”:datetime.datetime(2015,6,2,11,8,19,528000)}
2015-06-02 08:08:19-0300[示例]信息:十字轴已关闭(已完成)

我在查看网站的源代码,有更多的网页链接是爬网无法获取的,可能是我的规则不正确,所以,首先有几件事:

1)
rules
属性仅在扩展
CrawlSpider
类时有效,如果扩展更简单的
scrapy.Spider
,它们将不起作用

2) 如果您使用
规则
爬行器
路径,则不应覆盖默认的
解析
回调,因为默认实现实际上调用规则——因此您希望为回调使用另一个名称

3) 要实际提取所需的链接,可以在回调中使用一个函数从页面中刮取链接:

from scrapy.contrib.linkextractors import LinkExtractor

class MySpider(scrapy.Spider):
    ...

    def parse_links(self, response):
        extractor = LinkExtractor(allow=r'lattes\.cnpq\.br/\d+')
        for link in extractor.extract_links(response):
            item = LattesItem()
            item['url'] = link.url

我希望它能有所帮助。

您想提取这些链接还是要跟踪这些链接?根据您的需要,您需要以不同的方式进行处理。此外,如果您知道您想要的所有链接都在页面
http://www.ppgcc.ufv.br/?page_id=697
,您可以在
开始URL
中使用它。如果您粘贴代码,我们可能会帮助您更轻松。colega vi que vcébr,então fica mais facil de nos comunicarmos。。。。您的网站是否有最终的处理流程?学校所在地为梅斯莫·帕德劳大学,在该校的历史上一直保持着领先地位。cnpq.br/numeros que eu encontral dentro de qualquer pagina do siteoi,Bruno!:)听着,在这里用葡萄牙语写作是不礼貌的(内容应该对其他人也有帮助)。你可以用葡萄牙语用葡萄牙语问这个问题:我很接近,但再看看版本,也许我的规则是incorrect@BrunoCarazato我相信您只需要一条规则就可以了:
规则(SgmlLinkExtractor(allow=[r'.*')、callback='parse_links',follow=True)
@BrunoCarazato这告诉Scrapy:“只需跟踪域中的每个链接,为其调用parse_链接,并始终应用此规则即可