scrapy 为何无法爬数据?
由于业务需要,要爬托福100网站的数据。源代码如下,我是参照网上scrapy教程写的:
#coding:utf-8
from scrapy.spider import Spider,BaseSpider
from scrapy.http import Request
from scrapy.contrib.spiders import Rule,CrawlSpider
from scrapy.contrib.linkextractors import LinkExtractor
from tpo.items import TpoItem
from scrapy import log
class TPOSpider(Spider):
name = "tpo"
allowed_domains = ['http://toefl.100.com']
start_urls=[
"http://toefl.100.com/t/kouyu/1.html", #设置起始url
]
def parse(self,response):
url_list = list(set([ url for url in response.xpath('//a/@href').extract() if url.startswith('http://toefl.100.com/t/kouyu/')]))
self.url_list = url_list
for url in self.url_list:
print url
yield Request(url, callback=self.parse_item)
def parse_item(self,response):
print 'call !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
item = TpoItem()
title = ','.join(response.xpath('//h3[6]/text()').extract())
question = ','.join(response.xpath('//div[@class="c-english"]/h6/text()').extract())
analysis1 = ','.join(response.xpath('//div[@class="c-cont c-separate no-b"]/text()').extract())
analysis2 = ','.join(response.xpath('//div[@class="c-cont c-separate "]/text()').extract())
sample_anser_part1 = response.xpath('//div[@class="no-b c-separate"]/text()').extract()
sample_anser_part2 = response.xpath('//div[@class=" c-separate"]/text()').extract()
sample_anser_part1.extend(sample_anser_part2)
sample_anser = ','.join(sample_anser_part1)
item["title"] = title.encode('utf-8')
item["question"] =question.encode('utf-8')
item["analysis1"] = analysis1.encode('utf-8')
item["analysis2"] = analysis2.encode('utf-8')
item["sample_anser"] = sample_anser.encode('utf-8')
yield item
思路是:
parse
函数中先根据
start_urls
爬一个网页的数据,上面有所有要爬网页数据的url,然后把url都记录下来,然后对每一个url,都调用
parse_item
函数,取出里面的数据,
parse_item
里面定义了要取出哪些数据。
但是运行的时候根本就不会调用
parse_item
,这是运行的报告,请问错在什么地方?
D:\tpo\tpo>C:\Python27\Scripts\scrapy.exe crawl tpo
2014-09-28 16:25:14+0800 [scrapy] INFO: Scrapy 0.24.4 started (bot: tpo)
2014-09-28 16:25:14+0800 [scrapy] INFO: Optional features available: ssl, http
, django
2014-09-28 16:25:14+0800 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODUL
: 'tpo.spiders', 'SPIDER_MODULES': ['tpo.spiders'], 'BOT_NAME': 'tpo'}
2014-09-28 16:25:14+0800 [scrapy] INFO: Enabled extensions: LogStats, TelnetCo
ole, CloseSpider, WebService, CoreStats, SpiderState
2014-09-28 16:25:14+0800 [scrapy] INFO: Enabled downloader middlewares: HttpAu
Middleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, D
aultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, Redir
tMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-09-28 16:25:14+0800 [scrapy] INFO: Enabled spider middlewares: HttpErrorM
dleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddl
are
2014-09-28 16:25:14+0800 [scrapy] INFO: Enabled item pipelines: TpoPipeline
2014-09-28 16:25:14+0800 [tpo] INFO: Spider opened
2014-09-28 16:25:14+0800 [tpo] INFO: Crawled 0 pages (at 0 pages/min), scraped
items (at 0 items/min)
2014-09-28 16:25:14+0800 [scrapy] DEBUG: Telnet console listening on 127.0.0.1
023
2014-09-28 16:25:14+0800 [scrapy] DEBUG: Web service listening on 127.0.0.1:60
2014-09-28 16:25:15+0800 [tpo] DEBUG: Crawled (200) <GET http://toefl.100.com/
kouyu/1.html> (referer: None)
2014-09-28 16:25:15+0800 [tpo] DEBUG: Filtered offsite request to 'toefl.100.c
': <GET http://toefl.100.com/t/kouyu/380.html>
2014-09-28 16:25:15+0800 [tpo] INFO: Closing spider (finished)
2014-09-28 16:25:15+0800 [tpo] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 226,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 10365,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2014, 9, 28, 8, 25, 15, 163000),
'log_count/DEBUG': 4,
'log_count/INFO': 7,
'offsite/domains': 1,
'offsite/filtered': 202,
'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(2014, 9, 28, 8, 25, 14, 764000)}
2014-09-28 16:25:15+0800 [tpo] INFO: Spider closed (finished)
renhono
10 years, 3 months ago