【发布时间】:2014-12-05 06:13:34
【问题描述】:
我想从网站获取一些数据,所以我用scrapy写了一个蜘蛛,但是当我回调另一个“parse_zai”时,它似乎失败了,我该如何完成呢?请帮助!!
代码在这里
# encoding utf-8
from scrapy.http import Request
from scrapy.selector import Selector
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from tencentnews.items import TencentnewsItem
class Tencentnews_spider(CrawlSpider):
name = "Tnews"
allowed_domains = ["news.qq.com"]#It's the web i scrapyed
start_urls = [
"http://news.qq.com/china_index.shtml",
"http://news.qq.com/world_index.shtml",
"http://news.qq.com/society_index.shtml",
]
rules = [
Rule(SgmlLinkExtractor(allow=('/a/\d{8}/\d{6}\.htm',)),follow=True,callback='parse_item'),
Rule(SgmlLinkExtractor(allow=('/(.+)\.shtml', )), follow=True),
]
主要
def parse_item(self, response):
self.log('Hi, this is an item page! %s' % response.url)
sel = Selector(response)
item = TencentnewsItem()
item['articlename'] = sel.xpath("//div[@id='C-Main-Article-QQ']/div[1]/h1/text()").extract() #get the news'article
item['reportsource'] = sel.xpath("//span[@class='color-a-1']/a/text()").extract()
item['articletime'] = sel.xpath("//span[@class='article-time']/text()").extract()
item['commentnumber'] = sel.xpath("//a[@id='cmtNum']/text()").extract()
item['commenturl'] = sel.xpath("//a[@id='cmtNum']/@href").extract()
print repr(item).decode("unicode-escape") + '\n'
for url in item['commenturl']:
request = Request(url,callback = self.parse_zai)
request.meta['item'] = item
return request
def parse_zai(self,response):
print 'helloworld'
sel = Selector(response)
item = response.meta['item']
item['title'] = sel.xpath("//div[@class='bigTitle']/h1/a/text()").extract()
print repr(item).decode("unicode-escape") + '\n'
return item
【问题讨论】:
-
“似乎失败了”是什么意思?您收到的错误消息是什么?
-
查看网站后,我发现
commenturl域coral.qq.com/xxx现在在allowed_domains 中。将域添加到 allowed_domains,或仅删除 allow_domains 属性。 -
因为 parse_zai 不返回任何结果,即使是“helloworld”,太伤心了!