我正在尝试使用Expedia的API
从Expedia中提取房价和可用性数据。然而,我遇到的问题是,当酒店在某一天没有空房时,会弹出错误消息
基于以下代码使用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 + '¤cyCode=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提取数据,因为我认为我的方法实际上效率很低
也许我遗漏了什么,但我不知道你为什么在这里使用beautifulsoup。你应该试着把它切掉,然后像下面那样使用
urllib2
和json
对于错误处理,我只使用一些
try
块,例如:相关问题 更多 >
编程相关推荐