Python爬网结果以错误消息结束

2024-04-25 11:56:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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 + '&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提取数据,因为我认为我的方法实际上效率很低


Tags: fromimportdatetimetimeprivatekeyshotelcan
1条回答
网友
1楼 · 发布于 2024-04-25 11:56:44

也许我遗漏了什么,但我不知道你为什么在这里使用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"

相关问题 更多 >