2016-05-28 9 views
1

私はこのような質問をしましたScrapy can't get data。しかし、私は別のスパイダーを使うときに新しい問題があります。私はxpathに注意を払ってきましたが、このプログラムにも同じエラーがあるようです。ここでリンクをたどったときに治療がデータを取得できない

は私の蜘蛛のコードです:

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.selector import Selector 
from scrapy import Item, Field 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from DB_Connection import DB_Con 

class UniParc(Item): 
    database = Field() 
    identifier = Field() 
    version = Field() 
    organism = Field() 
    first_seen = Field() 
    last_seen = Field() 
    active = Field() 
    source = Field() 

class UniParcSpider(CrawlSpider): 
    name = "UniParc" 
    allowed_domains = ["uniprot.org"] 
    start_urls = ["http://www.uniprot.org/uniparc/?query=rna&offset=25&sort=score&columns=id%2corganisms%2ckb%2cfirst-seen%2clast-seen%2clength"] 

    rules = (
     Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=('//*[@id="results"]/tr/td[2]/a',)), callback="parse_items", follow = True), 
    ) 

    def parse_items(self, response): 
     hxs = Selector(response) 
     sites = hxs.xpath('//*[@id="results"]/tr') 
     db = DB_Con() 
     collection = db.getcollection(self.term) 
     for site in sites: 
      item = UniParc() 
      item["database"] = map(unicode.strip, site.xpath("td[1]/text()").extract()) 
      item["identifier"] = map(unicode.strip, site.xpath("td[2]/a/text()").extract()) 
      item["version"] = map(unicode.strip, site.xpath("td[3]/text()").extract()) 
      item["organism"] = map(unicode.strip, site.xpath("td[4]/a/text()").extract()) 
      item["first_seen"] = map(unicode.strip, site.xpath("td[5]/text()").extract()) 
      item["last_seen"] = map(unicode.strip, site.xpath("td[6]/text()").extract()) 
      item["active"] = map(unicode.strip, site.xpath("td[7]/text()").extract()) 
      item['source'] = self.name 
      collection.update({"identifier": item['identifier']}, dict(item), upsert=True) 
      yield item 

私はそれからのデータを追跡し、取得したいリンクを抽出するためにrulesを使用しました。しかし、start_urlからURLが取得されていないようです。ここで

はログです:

2016-05-28 22:28:54 [scrapy] INFO: Enabled item pipelines: 
2016-05-28 22:28:54 [scrapy] INFO: Spider opened 
2016-05-28 22:28:54 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 
2016-05-28 22:28:54 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023 
2016-05-28 22:28:55 [scrapy] DEBUG: Crawled (200) <GET http://www.uniprot.org/uniparc/?query=rna&offset=25&sort=score&columns=id%2corganisms%2ckb%2cfirst-seen%2clast-seen%2clength> (referer: None) 
2016-05-28 22:28:55 [scrapy] INFO: Closing spider (finished) 
2016-05-28 22:28:55 [scrapy] INFO: Dumping Scrapy stats: 
{'downloader/request_bytes': 314, 
'downloader/request_count': 1, 
'downloader/request_method_count/GET': 1, 
'downloader/response_bytes': 12263, 
'downloader/response_count': 1, 
'downloader/response_status_count/200': 1, 
'finish_reason': 'finished', 
'finish_time': datetime.datetime(2016, 5, 28, 14, 28, 55, 638618), 
'log_count/DEBUG': 2, 
'log_count/INFO': 7, 
'response_received_count': 1, 
'scheduler/dequeued': 1, 
'scheduler/dequeued/memory': 1, 
'scheduler/enqueued': 1, 
'scheduler/enqueued/memory': 1, 
'start_time': datetime.datetime(2016, 5, 28, 14, 28, 54, 645490)} 

だから、誰が私のコードで何が間違って伝えることができますか?私のxpathに何か問題がありますか?しかし、私はこれを何度もチェックしています。

答えて

0

だけ置き換え、XPath式を修正し、以下のリンクステップを固定するために:

//*[@id="results"]/tr/td[2]/a 

と:

//*[@id="results"]//tr/td[2]/a 

そして、サイドノートとして、あなたが挿入するべきではありません抽出されたアイテムはスパイダーのデータベースに直接格納されます。そのために、Scrapyにはpipelinesがあります。 MongoDBの場合は、scrapy-mongodbをチェックしてください。参照してください:

+0

[OK]を、WITH' // '私は' [ID @ = "結果"] * 'の下にあるすべての' tr'タグを取得することができますが、私が使用している場合、 '/ '、私は何を得ることができますか? 1つのリンクのみ? –

+0

そして、 '//'は他のタグを見落とすことを意味するので、// // [@ id = "results"] // tr/td [2]/a'と '/ tr/td [2]/a '同じ効果がありますか? –

+0

@Coding_Rabbit with/youは何も得ていないので、それに辿り着くリンクはありません。ありがとう。 – alecxe

関連する問題