2017-06-12 7 views
1

私はヤフーからのRSSフィードをこすりしようとしています(そのOpenカンパニーRSSフィード| https://developer.yahoo.com/finance/company.html)私は、次のURLこすりしようとしていますScrapy RSSスクレーパー

:何らかの理由で私のクモのISN」のhttps://feeds.finance.yahoo.com/rss/2.0/headline?s=BPMX

をそれは生成されたXPathか、parse_itemの定義に何らかの問題があるかもしれないということです。

import scrapy 
from scrapy.spiders import CrawlSpider 
from YahooScrape.items import YahooScrapeItem 

class Spider(CrawlSpider): 
    name= "YahooScrape" 
    allowed_domains = ["yahoo.com"] 
    start_urls = ('https://feeds.finance.yahoo.com/rss/2.0/headline?s=BPMX',) 

    def parse_item(self, response): 
     self.logger.info('Hi, this is an item page! %s', response.url) 
     item = EmperyscraperItem() 
     item['title'] = response.xpath('//*[@id="collapsible"]/div[1]/div[2]/span',).extract()    #define XPath for title 
     item['link'] = response.xpath('//*[@id="collapsible"]/div[1]/div[2]/span',).extract()     #define XPath for link 
     item['description'] = response.xpath('//*[@id="collapsible"]/div[1]/div[2]/span',).extract()   #define XPath for description 
     return item 

コードにはどのような問題がありますか?そうでない場合は、タイトル、desc、およびリンクを抽出するための適切なXPathの指示は何ですか。私はScrapyには新しく、ちょっとした助けが必要です。

編集:私はクモを更新し、以下に示すようにXMLFeedSpiderにそれを変換しました:

2017-06-13 11:25:57 [scrapy.core.engine] ERROR: Error while obtaining start requests 

任意のアイデア理由:

import scrapy 

from scrapy.spiders import XMLFeedSpider 
from YahooScrape.items import YahooScrapeItem 

class Spider(XMLFeedSpider): 
    name = "YahooScrape" 
    allowed_domains = ["yahoo.com"] 
    start_urls = ('https://feeds.finance.yahoo.com/rss/2.0/headline?s=BPMX') #Crawl BPMX 
    itertag = 'item' 

    def parse_node(self, response, node): 
     self.logger.info('Hi, this is a <%s> node!: %s', self.itertag, ''.join(node.extract())) 

     item = YahooScrapeItem() 
     item['title'] = node.xpath('item/title/text()',).extract()    #define XPath for title 
     item['link'] = node.xpath('item/link/text()').extract() 
     item['pubDate'] = node.xpath('item/link/pubDate/text()').extract() 
     item['description'] = node.xpath('item/category/text()').extract()    #define XPath for description 
     return item 

#Yahoo RSS feeds http://finance.yahoo.com/rss/headline?s=BPMX,APPL 

今、私は次のエラーを取得していますエラーが発生しましたか?私のHTMLパスが正しいように見えます。

答えて

1

私が見ることができるから、CrawlSpider only works for HTML responses。だから私はあなたがより簡単にscrapy.Spider、またはより特化したXMLFeedSpiderを構築することをお勧めします。

次に、parse_itemsで使用しているXPathは、ブラウザがXML/RSSフィードからHTMLとしてレンダリングしたものから構築されたようです。 フィードに*[@id="collapsible"]または<div>がありません。代わりにview-source:https://feeds.finance.yahoo.com/rss/2.0/headline?s=BPMX

ルック:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<rss version="2.0"> 
    <channel> 
     <copyright>Copyright (c) 2017 Yahoo! Inc. All rights reserved.</copyright> 
     <description>Latest Financial News for BPMX</description> 
     <image> 
      <height>45</height> 
      <link>http://finance.yahoo.com/q/h?s=BPMX</link> 
      <title>Yahoo! Finance: BPMX News</title> 
      <url>http://l.yimg.com/a/i/brand/purplelogo/uh/us/fin.gif</url> 
      <width>144</width> 
     </image> 
     <item> 
      <description>MENLO PARK, Calif., June 7, 2017 /PRNewswire/ -- BioPharmX Corporation (NYSE MKT: BPMX), a specialty pharmaceutical company focusing on dermatology, today announced that it will release its financial results ...</description> 
      <guid isPermaLink="false">f56d5bf8-f278-37fd-9aa5-fe04b2e1fa53</guid> 
      <link>https://finance.yahoo.com/news/biopharmx-report-first-quarter-financial-101500259.html?.tsrc=rss</link> 
      <pubDate>Wed, 07 Jun 2017 10:15:00 +0000</pubDate> 
      <title>BioPharmX to Report First Quarter Financial Results</title> 
     </item> 

の作業クモの例は:

import scrapy 

from scrapy.spiders import XMLFeedSpider 
#from YahooScrape.items import YahooScrapeItem 

class Spider(XMLFeedSpider): 
    name = "YahooScrape" 
    allowed_domains = ["yahoo.com"] 
    start_urls = ('https://feeds.finance.yahoo.com/rss/2.0/headline?s=BPMX',) #Crawl BPMX 
    itertag = 'item' 

    def parse_node(self, response, node): 
     self.logger.info('Hi, this is a <%s> node!: %s', self.itertag, ''.join(node.extract())) 

     item = {} 
     item['title'] = node.xpath('title/text()',).extract_first()    #define XPath for title 
     item['link'] = node.xpath('link/text()').extract_first() 
     item['pubDate'] = node.xpath('link/pubDate/text()').extract_first() 
     item['description'] = node.xpath('description/text()').extract_first()    #define XPath for description 
     return item 
+0

私はXMLFeedSpiderに変更し、私はパスの右の構文を持っていると思います。何らかの理由で私はstart_requestsを適切に定義することができません。多分私は何かを欠いているでしょうか? – Friezan

+0

XPathで 'item /'接頭辞を削除するとどうなりますか? –

+0

残念ながら同じ問題です。何か案は? – Friezan