Skip to main content
 首页 » 编程设计

python之Scrapy,从 StubHub 抓取价格数据

2025年05月04日13Renyi-Fan

我在这方面遇到了困难。

我想抓取在好莱坞露天剧场举行的 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 

如果有任何帮助,我将不胜感激 我已经为此苦苦挣扎了很长一段时间。 提前致谢!

请您参考如下方法:

根据 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, 
 ... 
] 

希望对您有所帮助。