1

リンクをたどるために、Scrapyを特定のXPathの場所に制限しようとしています。XPathは正しいですが(Chrome用のXPath Helperプラグインによると)、Crawl Spiderを実行すると、ルールで構文エラーが発生します。

私のスパイダーコードは次のとおりです。

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from tutorial.items import BassItem

import logging
from scrapy.log import ScrapyFileLogObserver

logfile = open('testlog.log', 'w')
log_observer = ScrapyFileLogObserver(logfile, level=logging.DEBUG)
log_observer.start()


class BassSpider(CrawlSpider):
    name = "bass"
    allowed_domains = ["talkbass.com"]
    start_urls = ["http://www.talkbass.com/forum/f126"]


    rules = [Rule(SgmlLinkExtractor(allow=['/f126/index*']), callback='parse_item', follow=True, restrict_xpaths=('//a[starts-with(@title,"Next ")]')]


    def parse_item(self, response):

        hxs = HtmlXPathSelector(response)


        ads = hxs.select('//table[@id="threadslist"]/tbody/tr/td[@class="alt1"][2]/div')
        items = []
        for ad in ads:
            item = BassItem()
            item['title'] = ad.select('a/text()').extract()
            item['link'] = ad.select('a/@href').extract()
            items.append(item)
        return items

したがって、ルール内では、XPath'// a [starts-with(@title、 "Next")]'がエラーを返しますが、実際のXPathが有効であるため、理由はわかりません。スパイダーに各「次のページ」リンクをクロールさせようとしているだけです。誰かが私を助けることができますか?私のコードの他の部分が必要な場合はお知らせください。

4

1 に答える 1

1

問題となるのはxpathではなく、完全なルールの構文が正しくないことです。次のルールは構文エラーを修正しますが、必要なことを実行していることを確認するためにチェックする必要があります。

rules = (Rule(SgmlLinkExtractor(allow=['/f126/index*'], restrict_xpaths=('//a[starts-with(@title,"Next ")]')), 
        callback='parse_item', follow=True, ),
)

一般的なポイントとして、質問に実際のエラーを投稿することを強くお勧めします。これは、エラーの認識と実際のエラーが大きく異なる可能性があるためです。

于 2013-01-18T08:45:59.240 に答える