且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解析scrapy中的JSON数据

更新时间:2023-02-23 16:49:55

让我们以与上述链接对应的所有票证信息位于此处,并且从这里,您只需要将json的字段映射到html(html页面中的票务信息).

all ticket information corresponds to the above link is located here and from there you just need to map the fields of the json to html( ticket info in the html page).

In [1]: ticket_info = jsonresponse.get('tickets')

In [2]: ticket_info
Out[2]: 
 [{u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'1',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB844757004',
  u'ind': u'1',
  u'l': u'GA Main Floor',
  u'n': u'Instant Download.',
  u'p': u'27.00',
  u'q': u'3',
  u'r': u'G2',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'1',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'1',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB891598272',
  u'ind': u'1',
  u'l': u'GA Main Floor',
  u'n': u'Instant Download.',
  u'p': u'29.00',
  u'q': u'1',
  u'r': u'G3',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'0',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB900500475',
  u'ind': u'0',
  u'l': u'GA MAIN FLOOR',
  u'n': u'',
  u'p': u'30.00',
  u'q': u'2',
  u'r': u'G4',
  u'rhdn': u'0',
  u's': u'GA MAIN F..',
  u'sd': u'0',
  u't': u'0',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30613',
  u'd': u'3000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB886172318',
  u'ind': u'1',
  u'l': u'GA Second Balcony - Limited View',
  u'n': u'eTicket,Obstructed/Limited View Instant Download.',
  u'p': u'37.00',
  u'q': u'1',
  u'r': u'GA',
  u'rhdn': u'0',
  u's': u'GA Second Balcony - Limited View',
  u'sd': u'0',
  u't': u'1',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'1',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB806449210',
  u'ind': u'1',
  u'l': u'GA MAIN FLOOR',
  u'n': u'eTicket Instant Download.',
  u'p': u'39.00',
  u'q': u'8',
  u'r': u'GA',
  u'rhdn': u'0',
  u's': u'GA MAIN F..',
  u'sd': u'0',
  u't': u'1',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30612',
  u'd': u'2000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB900500473',
  u'ind': u'1',
  u'l': u'GA First Balcony',
  u'n': u'Instant Download.',
  u'p': u'46.00',
  u'q': u'2',
  u'r': u'G3',
  u'rhdn': u'0',
  u's': u'GA First Balcony',
  u'sd': u'0',
  u't': u'0',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB893201517',
  u'ind': u'1',
  u'l': u'GA Main Floor',
  u'n': u'Instant Download.',
  u'p': u'50.00',
  u'q': u'6',
  u'r': u'G5',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'3',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30615',
  u'd': u'995',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'07/19/15',
  u'i': u'VB900847659',
  u'ind': u'0',
  u'l': u'GA',
  u'n': u'Tickets will be ready for delivery by 07/19/2015.',
  u'p': u'53.00',
  u'q': u'4',
  u'r': u'GA',
  u'rhdn': u'0',
  u's': u'GA',
  u'sd': u'0',
  u't': u'0',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30615',
  u'd': u'995',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'07/19/15',
  u'i': u'VB900847710',
  u'ind': u'0',
  u'l': u'GA',
  u'n': u'Tickets will be ready for delivery by 07/19/2015.',
  u'p': u'53.00',
  u'q': u'3',
  u'r': u'GA',
  u'rhdn': u'0',
  u's': u'GA',
  u'sd': u'0',
  u't': u'0',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30612',
  u'd': u'2000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB900500474',
  u'ind': u'1',
  u'l': u'GA First Balcony',
  u'n': u'Instant Download.',
  u'p': u'57.00',
  u'q': u'8',
  u'r': u'G2',
  u'rhdn': u'0',
  u's': u'GA First Balcony',
  u'sd': u'0',
  u't': u'3',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB880887546',
  u'ind': u'0',
  u'l': u'GA Main Floor',
  u'n': u'eTicket',
  u'p': u'61.00',
  u'q': u'4',
  u'r': u'GA',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'1',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB893198548',
  u'ind': u'1',
  u'l': u'GA Main Floor',
  u'n': u'Instant Download.',
  u'p': u'85.00',
  u'q': u'8',
  u'r': u'G4',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'3',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'0',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB893198623',
  u'ind': u'1',
  u'l': u'GA Main Floor',
  u'n': u'Instant Download.',
  u'p': u'94.00',
  u'q': u'2',
  u'r': u'G4',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'0',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'1',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB844757006',
  u'ind': u'1',
  u'l': u'GA Main Floor',
  u'n': u'Instant Download.',
  u'p': u'88.00',
  u'q': u'8',
  u'r': u'G3',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'1',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30615',
  u'd': u'995',
  u'e': u'0',
  u'f': u'0',
  u'g': u'0',
  u'h': u'07/19/15',
  u'i': u'VB793104621',
  u'ind': u'0',
  u'l': u'GA',
  u'n': u'Tickets will be ready for delivery by 07/19/2015.',
  u'p': u'91.00',
  u'q': u'8',
  u'r': u'GA',
  u'rhdn': u'0',
  u's': u'GA',
  u'sd': u'0',
  u't': u'3',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'1',
  u'f': u'1',
  u'g': u'0',
  u'h': u'',
  u'i': u'VB844757008',
  u'ind': u'1',
  u'l': u'GA Main Floor',
  u'n': u'Instant Download.',
  u'p': u'111.00',
  u'q': u'8',
  u'r': u'G4',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'1',
  u'v': u'',
  u'z': u'0'},
 {u'c': u'30611',
  u'd': u'1000',
  u'e': u'0',
  u'f': u'0',
  u'g': u'0',
  u'h': u'07/16/15',
  u'i': u'VB791993584',
  u'ind': u'0',
  u'l': u'GA Main Floor',
  u'n': u'Zone Seating. The seller is committing to procure these tickets for you upon receipt of your order. After you place your order and your order is confirmed, we guarantee that your tickets will be within the listed zone or section listed or one comparable and that you will receive these tickets in time for the event or your money back. Orders exceeding four tickets may be split up into different rows within the requested zone or section.',
  u'p': u'170.00',
  u'q': u'6',
  u'r': u'GA',
  u'rhdn': u'0',
  u's': u'GA Main Floor',
  u'sd': u'0',
  u't': u'3',
  u'v': u'',
  u'z': u'1'}]

ticket_info是从低价到高价的票证信息列表.现在,从上面的列表中仅提取price-info. p是对应于机票价格的键.使用这些信息,您将获得类似的信息

the ticket_info is the list of ticket-information ranging from low price to high price. Now extract only the price-info from the above list. p is the key corresponds to the price of the ticket. Using these information it will give you something like this,

In [3]: price_list = [i.get('p') for i in ticket_info]

In [4]: price_list 
Out[4]: 
[u'27.00',
 u'29.00',
 u'30.00',
 u'37.00',
 u'39.00',
 u'46.00',
 u'50.00',
 u'53.00',
 u'53.00',
 u'57.00',
 u'61.00',
 u'85.00',
 u'94.00',
 u'88.00',
 u'91.00',
 u'111.00',
 u'170.00']

为了确保您能获得最低的价格,您可以执行price_list.sort()并采用其中的第一个元素.

in order to make sure that you will get the lowest price you can perform price_list.sort() and take the first element in that.