Scrapy,从StubHub抓取价格数据

时间:2014-03-31 19:52:16

标签: python scrapy screen-scraping

我在这个问题上遇到了困难。

我想在好莱坞露天剧场为这场Bruno Mars演唱会列出所有价格,以便我得到平均价格。

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

任何帮助都会非常感激我已经在这个问题上挣扎了很长一段时间了。 提前谢谢!

1 个答案:

答案 0 :(得分: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,
 ...
]

希望有所帮助。