2014-03-31 53 views
2

我一直在這個困難的時間。Scrapy,從StubHub刮來的價格數據

我想刮掉在好萊塢碗的布魯諾火星演唱會上列出的所有價格,這樣我就可以得到平均價格。

http://www.stubhub.com/bruno-mars-tickets/bruno-mars-hollywood-hollywood-bowl-31-5-2014-4449604/

我已經位於HTML中的價格和XPath是非常簡單的,但我不能讓任何值返回。

我認爲它與通過javascript或ajax生成的內容有關,但我無法弄清楚如何發送正確的請求來使代碼正常工作。

這是我有:

from scrapy.spider import BaseSpider 
from scrapy.selector import Selector 

from deeptix.items import DeeptixItem 

class TicketSpider(BaseSpider): 
    name = "deeptix" 
    allowed_domains = ["stubhub.com"] 
    start_urls = ["http://www.stubhub.com/bruno-mars-tickets/bruno-mars-hollywood-hollywood-bowl-31-5-2014-4449604/"] 

def parse(self, response): 
    sel = Selector(response) 
    sites = sel.xpath('//div[contains(@class, "q_cont")]') 
    items = [] 
    for site in sites: 
     item = DeeptixItem() 
     item['price'] = site.xpath('span[contains(@class, "q")]/text()').extract() 
     items.append(item) 
    return items 

任何幫助將不勝感激我一直是這樣一個苦苦掙扎相當一段時間。 預先感謝您!

+0

我想你想要的是從返回JSON的API調用中獲取:https://www.stubhub.com/ticketAPI/restSvc/event/4449604/sort/price/0?ts=1396358054406 –

回答

1

根據chrome網絡控制檯,有一個AJAX請求載入關於事件的所有信息,包括票據信息。

你不需要scrapy所有,只是urllib2來獲取數據和json模塊提取門票價格:

import json 
from pprint import pprint 
import urllib2 

url = 'http://www.stubhub.com/ticketAPI/restSvc/event/4449604' 

data = json.load(urllib2.urlopen(url)) 
tickets = data['eventTicketListing']['eventTicket'] 

prices = [ticket['tc']['amount'] for ticket in tickets] 
pprint(sorted(prices)) 

打印:

[156.0, 
159.0, 
169.0, 
175.0, 
175.0, 
194.5, 
199.0, 
... 
] 

希望有所幫助。

+0

謝謝,但有幫助但我試圖讓所有事件都做到這一點,我的API有一個請求限制。我正在考慮使用scrapy爲這個和其他一些項目處理AJAX請求。我很欣賞這種迴應 – user3204659