1

これが私のコードです。誰か助けてください。何らかの理由で、スパイダーは実行されますが、実際にはフォーラムのスレッドをクロールしません。開始 URL の特定のフォーラムのフォーラム スレッド内のすべてのテキストを抽出しようとしています。

from scrapy.spider import BaseSpider

from scrapy.contrib.spiders import CrawlSpider, Rule

from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

from scrapy.selector import HtmlXPathSelector

from xbox.items import xboxItem

from scrapy.item import Item
from scrapy.conf import settings


class xboxSpider(CrawlSpider):
    name = "xbox"
    allowed_domains = ["forums.xbox.com"]
    start_urls= [
    "http://forums.xbox.com/xbox_forums/xbox_360_games/e_k/gearsofwar3/default.aspx",
    ]
    rules= [
        Rule(SgmlLinkExtractor(allow=['/t/\d+']),callback='parse_thread'),  
        Rule(SgmlLinkExtractor(allow=('/t/new\?new_start=\d+',)))
            ]


    def parse_thread(self, response):
        hxs=HtmlXPathSelector(response)

        item=xboxItem()
        item['content']=hxs.selec("//div[@class='post-content user-defined-markup']/p/text()").extract()
        item['date']=hxs.select("//span[@class='value']/text()").extract()
        return item

ログ出力:

2013-03-13 11:22:18-0400 [scrapy] DEBUG: Enabled item pipelines: 
2013-03-13 11:22:18-0400 [xbox] INFO: Spider opened 
2013-03-13 11:22:18-0400 [xbox] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 
2013-03-13 11:22:18-0400 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023 
2013-03-13 11:22:18-0400 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-03-13 11:22:20-0400 [xbox] DEBUG: Crawled (200) <GET forums.xbox.com/xbox_forums/xbox_360_games/e_k/gearsofwar3/f/…; (referer: None) 
2013-03-13 11:22:20-0400 [xbox] DEBUG: Filtered offsite request to 'forums.xbox.com': <GET forums.xbox.com/xbox_forums/xbox_360_games/e_k/gearsofwar3/f/…; 
2013-03-13 11:22:20-0400 [xbox] INFO: Closing spider (finished) 
2013-03-13 11:22:20-0400 [xbox] INFO: Dumping spider stats
4

1 に答える 1

1

最初の微調整として、最初のルールを「.」を付けて変更する必要があります。次のように、正規表現の開始時に。また、開始 URL をフォーラムの実際の最初のページに変更しました。

start_urls= [
"http://forums.xbox.com/xbox_forums/xbox_360_games/e_k/gearsofwar3/f/310.aspx",
]
rules = (
    Rule(SgmlLinkExtractor(allow=('./t/\d+')), callback="parse_thread", follow=True),
    Rule(SgmlLinkExtractor(allow=('./310.aspx?PageIndex=\d+')), ),
    )

スパイダーがスレッド内のすべてのページをクロールするようにルールを更新しました。

編集: 問題を引き起こしている可能性のあるタイプミスを発見し、日付の xpath を修正しました。

 item['content']=hxs.selec("//div[@class='post-content user-defined-markup']/p/text()").extract()
 item['date']=hxs.select("(//div[@class='post-author'])[1]//a[@class='internal-link view-post']/text()").extract()

上の行には「hxs.select」とあり、「hxs.select」である必要があります。それを変更したところ、コンテンツがスクレイピングされていることがわかりました。試行錯誤を重ねて (私は xpath に少し慣れていません)、最初の投稿の日付 (つまり、スレッドが作成された日付) を取得できたので、これですべて機能するはずです。

于 2013-03-13T13:37:35.650 に答える