scrapy 爬虫

scrapy是基于python的爬虫.

根据官方说明,在ubuntu下直接用sudo apt-get install Scarpy 会有问题.
于是根据说明,用下面的方法来安装: ( http://doc.scrapy.org/en/1.0/topics/ubuntu.html#topics-ubuntu)

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 627220E7
echo 'deb http://archive.scrapy.org/ubuntu scrapy main' | sudo tee /etc/apt/sources.list.d/scrapy.list
sudo apt-get update && sudo apt-get install scrapy

安装完以后,可以从一个简单的实例开始:
scrapy startproject helloworld

然后进入helloworld/helloworld/spider , 新建一个文件hellospider.py, 内容如下:

import scrapy

class HelloSpider(scrapy.Spider):
    name = "hellospider"
    allowed_domains = ["bagualu.net"]
    start_urls = [
        "http://www.bagualu.net/wordpress/"
    ]

    def parse(self, response):
        print "got url " + response.url
        print "got titile " + response.xpath('//title/text()').extract()[0]

然后回到helloworld目录下运行爬虫,得到的结果如下:

xuyang@xuyang-ubuntu:~/python_scrapy/helloworld$ scrapy crawl hellospider
2015-12-17 21:17:19 [scrapy] INFO: Scrapy 1.0.3 started (bot: helloworld)
2015-12-17 21:17:19 [scrapy] INFO: Optional features available: ssl, http11
2015-12-17 21:17:19 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'helloworld.spiders', 'SPIDER_MODULES': ['helloworld.spiders'], 'BOT_NAME': 'helloworld'}
2015-12-17 21:17:19 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2015-12-17 21:17:19 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-12-17 21:17:19 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-12-17 21:17:19 [scrapy] INFO: Enabled item pipelines: 
2015-12-17 21:17:19 [scrapy] INFO: Spider opened
2015-12-17 21:17:19 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-12-17 21:17:19 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-12-17 21:17:19 [scrapy] DEBUG: Crawled (200)  (referer: None)
got url http://www.bagualu.net/wordpress/
got titile   猎数博客 
2015-12-17 21:17:19 [scrapy] INFO: Closing spider (finished)
2015-12-17 21:17:19 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 223,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 7617,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2015, 12, 17, 13, 17, 19, 757758),
 'log_count/DEBUG': 2,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2015, 12, 17, 13, 17, 19, 146105)}
2015-12-17 21:17:19 [scrapy] INFO: Spider closed (finished)


本文地址: http://www.bagualu.net/wordpress/archives/5067 转载请注明




发表评论

电子邮件地址不会被公开。 必填项已用*标注