私は、ウェブサイトのRSSフィードをクロールし、記事のメタタグを解析するためにクロールスパイダーをプログラムしようとしています。ルールを正しく使用するには、restrict_xpathsを使用してURLをクロールし、解析します。
最初のRSSページは、RSSカテゴリを表示するページです。タグがタグの中にあるので、リンクを抽出することができました。それはこのようになります:あなたは私は、XPathとのリンクを取得することができます見ることができるように
<li class="regularitem">
<h4 class="itemtitle">
<a href="http://example.com/article1">article1</a>
</h4>
</li>
<li class="regularitem">
<h4 class="itemtitle">
<a href="http://example.com/article2">article2</a>
</h4>
</li>
:それはあなたにこのように見えることRSSカテゴリの記事をもたらすそのリンクをクリックしたら
<tr>
<td class="xmlLink">
<a href="http://feeds.example.com/subject1">subject1</a>
</td>
</tr>
<tr>
<td class="xmlLink">
<a href="http://feeds.example.com/subject2">subject2</a>
</td>
</tr>
再び私がタグを使用する場合 クローラがそのタグの中のリンクに行き、私のためにメタタグを解析するようにします。ここで
は私のクローラコードです:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from tutorial.items import exampleItem
class MetaCrawl(CrawlSpider):
name = 'metaspider'
start_urls = ['http://example.com/tools/rss'] # urls from which the spider will start crawling
rules = [Rule(SgmlLinkExtractor(restrict_xpaths=('//td[@class="xmlLink"]')), follow=True),
Rule(SgmlLinkExtractor(restrict_xpaths=('//h4[@class="itemtitle"]')), callback='parse_articles')]
def parse_articles(self, response):
hxs = HtmlXPathSelector(response)
meta = hxs.select('//meta')
items = []
for m in meta:
item = exampleItem()
item['link'] = response.url
item['meta_name'] =m.select('@name').extract()
item['meta_value'] = m.select('@content').extract()
items.append(item)
return items
私はクローラを実行したときしかし、これが出力されます:
DEBUG: Crawled (200) <GET http://http://feeds.example.com/subject1> (referer: http://example.com/tools/rss)
DEBUG: Crawled (200) <GET http://http://feeds.example.com/subject2> (referer: http://example.com/tools/rss)
私はここで間違って何をしているのですか?私は何度も何度もドキュメントを読んできましたが、何かを見落としているような気がします。どんな助けもありがとう。
EDIT:追加:items.append(アイテム)。元の投稿でそれを忘れていた。 EDIT::私もこれを試みたのだが、同じ出力の結果:あなたは、空のitems
を戻ってき
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from reuters.items import exampleItem
from scrapy.http import Request
class MetaCrawl(CrawlSpider):
name = 'metaspider'
start_urls = ['http://example.com/tools/rss'] # urls from which the spider will start crawling
rules = [Rule(SgmlLinkExtractor(allow=[r'.*',], restrict_xpaths=('//td[@class="xmlLink"]')), follow=True),
Rule(SgmlLinkExtractor(allow=[r'.*'], restrict_xpaths=('//h4[@class="itemtitle"]')),follow=True),]
def parse(self, response):
hxs = HtmlXPathSelector(response)
meta = hxs.select('//td[@class="xmlLink"]/a/@href')
for m in meta:
yield Request(m.extract(), callback = self.parse_link)
def parse_link(self, response):
hxs = HtmlXPathSelector(response)
meta = hxs.select('//h4[@class="itemtitle"]/a/@href')
for m in meta:
yield Request(m.extract(), callback = self.parse_again)
def parse_again(self, response):
hxs = HtmlXPathSelector(response)
meta = hxs.select('//meta')
items = []
for m in meta:
item = exampleItem()
item['link'] = response.url
item['meta_name'] = m.select('@name').extract()
item['meta_value'] = m.select('@content').extract()
items.append(item)
return items
ルールを編集しようとしました: ルール(SgmlLinkExtractor(allow = [r '。*')、restrict_xpaths =( '// td [@ class = "xmlLink"]'))、follow = True )、 ルール(SgmlLinkExtractor(allow = [r '。*']、restrict_xpaths =( 'h4 [@ class = "itemtitle"]'))、コールバック= 'parse_articles') しかし、出力。 – Marc
こんにちはマーク:どうやってその問題を最終的に解決しましたか?私が治療の例を実行すると、すべてのことがきちんとしているし、私のプロジェクトに適用されたロジックがルールが起動しないようだ... – hugsbrugs
は、 'scrapy.Spider'からあなたのスパイダーを継承しています。 – lhe