CrawlSpider を拡張するスパイダーを作成し、http: //scrapy.readthedocs.org/en/latest/topics/spiders.html のアドバイスに従いました。
問題は、開始 URL (たまたまホスト名と一致する) とそれが含むいくつかのリンクの両方を解析する必要があることです。
だから私は次のようなルールを定義しました: rules = [Rule(SgmlLinkExtractor(allow=['/page/d+']), callback='parse_items', follow=True)]
、しかし何も起こりません。
次に、次のような一連のルールを定義しようとしましたrules = [Rule(SgmlLinkExtractor(allow=['/page/d+']), callback='parse_items', follow=True), Rule(SgmlLinkExtractor(allow=['/']), callback='parse_items', follow=True)]
。問題は、スパイダーがすべてを解析することです。
スパイダーに _start_url_ とそれに含まれるいくつかのリンクのみを解析するように指示するにはどうすればよいですか?
アップデート:
メソッドをオーバーライドしようとしたparse_start_url
ので、開始ページからデータを取得できるようになりましたが、次のように定義されたリンクにはまだ従いませんRule
:
class ExampleSpider(CrawlSpider):
name = 'TechCrunchCrawler'
start_urls = ['http://techcrunch.com']
allowed_domains = ['techcrunch.com']
rules = [Rule(SgmlLinkExtractor(allow=['/page/d+']), callback='parse_links', follow=True)]
def parse_start_url(self, response):
print '++++++++++++++++++++++++parse start url++++++++++++++++++++++++'
return self.parse_links(response)
def parse_links(self, response):
print '++++++++++++++++++++++++parse link called++++++++++++++++++++++++'
articles = []
for i in HtmlXPathSelector(response).select('//h2[@class="headline"]/a'):
article = Article()
article['title'] = i.select('./@title').extract()
article['link'] = i.select('./@href').extract()
articles.append(article)
return articles