【发布时间】:2014-02-01 10:13:32
【问题描述】:
如果我不实施任何规则,我正在使用scrapy 做一个蜘蛛,但现在我正在尝试实施一个规则来获取分页器并抓取所有其余页面。但我不知道为什么我不能实现它。
蜘蛛码:
allowed_domains = ['guia.bcn.cat']
start_urls = ['http://guia.bcn.cat/index.php?pg=search&q=*:*']
rules = (
Rule(SgmlLinkExtractor(allow=("index.php?pg=search&from=10&q=*:*&nr=10"),
restrict_xpaths=("//div[@class='paginador']",))
, callback="parse_item", follow=True),)
def parse_item(self, response)
...
另外,我尝试在规则的允许参数中设置“index.php”,但都不起作用。
我在 scrapy 群组中看到我没有放“a/”或“a/@href”,因为 SgmlLinkExtractor 会自动搜索链接。
控制台输出似乎运行良好,但没有得到任何东西。
有什么想法吗?
提前致谢
编辑:
使用此代码可以工作
from scrapy.selector import Selector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from bcncat.items import BcncatItem
import re
class BcnSpider(CrawlSpider):
name = 'bcn'
allowed_domains = ['guia.bcn.cat']
start_urls = ['http://guia.bcn.cat/index.php?pg=search&q=*:*']
rules = (
Rule(
SgmlLinkExtractor(
allow=(re.escape("index.php")),
restrict_xpaths=("//div[@class='paginador']")),
callback="parse_item",
follow=True),
)
def parse_item(self, response):
self.log("parse_item")
sel = Selector(response)
i = BcncatItem()
#i['domain_id'] = sel.xpath('//input[@id="sid"]/@value').extract()
#i['name'] = sel.xpath('//div[@id="name"]').extract()
#i['description'] = sel.xpath('//div[@id="description"]').extract()
return i
【问题讨论】:
标签: python web-crawler scrapy