【问题标题】:Python - Crawl Result Ended Up with Error MessagesPython - 抓取结果以错误消息结尾
【发布时间】:2015-11-05 09:01:36
【问题描述】:

我正在尝试使用他们的APIExpedia 提取房价和空房数据。但是,我遇到的问题是,当酒店在某一天没有空房时,会弹出错误消息。

当我根据以下代码使用Print JD时的错误信息是:

{u'EanWsError': {u'category': u'SOLD_OUT', u'exceptionConditionId': -1, u'handling': u'RECOVERABLE', u'itineraryId': -1, u'ErrorAttributes': {u'errorAttributesMap': {u'entry': {u'value': 8001, u'key': u'SUPPLIER_ERROR_CODE'}}}, u'verboseMessage': u'errors.supplier.hotel.nolonger; error from supplier; 8001', u'presentationMessage': u'***The hotel you selected is no longer available. Please choose another.***'}, u'hotelId': 447643, u'customerSessionId': u'0AB2902D-92F0-CC91-50D2-6AA567897340', u'@size': u'0'}

import urllib2
import requests
import md5
import time
import datetime
import json
import csv
from datetime import date
from datetime import timedelta

#GENERATING SPECIAL KEYS FOR URL (GET)
apiKey = 'xxxxxxxxxxxxxxx' #private keys, can't disclose
secret = 'xxxxxxxxxxxxxxx' #private keys, can't disclose
cid = 'xxxxxxxxxxxxxxx' #private keys, can't disclose

hash = md5.new()
timestamp = str(int(time.time()))
sig = md5.new(apiKey + secret + timestamp).hexdigest()

#Date and Hotel Variables
StartDate = '12/31/2015'
EndDate = '12/31/2015'
HotelIDs = '447643'

url = 'http://api.ean.com/ean-services/rs/hotel/v3/avail?apiKey=' + apiKey + '&sig=' + sig + '&cid=' + cid + '&currencyCode=TWD&hotelId='+ HotelIDs + '&arrivalDate=' + StartDate + '&departureDate=' + EndDate + '&room1=1'
res = requests.get(url)
jd = json.loads(res.text)['HotelRoomAvailabilityResponse']

Name = jd['hotelName']

for Rooms in jd['HotelRoomResponse']:
    Descp = Rooms['rateDescription']
    Avail = Rooms['currentAllotment']
    Rate = Rooms['RateInfo']['ChargeableRateInfo']['@nightlyRateTotal']

    print Name, Descp, Avail, Rate

我该如何解决这个问题?

有没有更简单的方法来使用 python 提取数据,因为我认为我的方法实际上效率很低。

【问题讨论】:

    标签: python json api


    【解决方案1】:

    也许我遗漏了一些东西,但我不确定你为什么在这里使用 beautifulsoup。您应该尝试将其剪掉并使用urllib2json,如下所示。

    url = 'http://api.ean.com/ean-services/rs/hotel/v3/avail?apiKey=' + apiKey + '&sig=' + sig + '&cid=' + cid + '&currencyCode=TWD&hotelId='+ HotelIDs + '&arrivalDate=' + StartDate + '&departureDate=' + EndDate + '&room1=1'
    jd = json.load(urllib2.urlopen(url))
    

    那么对于错误处理,我将只使用一些 try 块,例如:

    try:
        Name = jd['hotelName']
    
        for Rooms in jd['HotelRoomResponse']:
              Descp = Rooms['rateDescription']
              Avail = Rooms['currentAllotment']
              Rate = Rooms['RateInfo']['ChargeableRateInfo']['@nightlyRateTotal']
    
              print Name, Descp, Avail, Rate
     except:
         try:
              Error = jd['hotelId']
              print "Error with " + Error
         except:
              print "unknown Error"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多