2011-07-10 73 views
1

有一個問題,我想將輸出添加到csv文件,但它不會在字段名稱下面開始,它將放置在下一行而不是在csv文件中填充playerMins項目時將其放置在第2行。有人可以告訴我我的代碼出錯了嗎?那就是:使用python scrapy將項目輸出到csv文件 - 問題如何在csv文件中輸出

class EspnSpider3(BaseSpider): 
    name = "espn3.org" 
    allowed_domains = ["espn3.org"] 
    start_urls = [ 
     "http://scores.espn.go.com/nba/boxscore?gameId=310502004" 

    ] 

    def parse(self, response): 
     hxs = HtmlXPathSelector(response) 
     item = EspnItem() 
     rows = [] 
     playerName = [] 
     playerMins = [] 

     # player names 
     p_names = hxs.select('(//table[@class="mod-data"][1]/tbody/tr)//a/text()').extract() 
     for p_name in p_names: 
      print p_name 
      yield EspnItem(playerName=p_name) 

     # minutes 
     p_minutes = hxs.select('(//table[@class="mod-data"][1]/tbody/tr)/td[2]').extract() 
     for p_minute in p_minutes: 
      print p_minute 
      yield EspnItem(playerMins=p_minute) 
+3

哪裏是你的代碼輸出到CSV? – bernie

+0

scrapy抓取espn3.org --set FEED_URI = items.csv --set FEED_FORMAT = csv –

+0

只是上面的標準c –

回答

2

能夠解決我的問題,經過多次使用Google和RTFM:Trying to Use an ItemExporter in Scrapy

這是我工作的代碼:

def parse(self, response): 
    hxs = HtmlXPathSelector(response) 
    player_names = hxs.select('(//table[@class="mod-data"][1]/tbody/tr)') 
    for p_name in player_names: 
     l = XPathItemLoader(item=EspnItem(), selector=p_name) 
     l.add_xpath('playerName', 'td[1]/a/text()') 
     l.add_xpath('playerMins', 'td[2]') 
     yield l.load_item()