我有以下 JSON,我正在使用 REST API 获取
看起来像这样
"itineraryIntent": {
"itineraryId": 2446,
"userNodes": [
{
"user": {
"id": "-2",
"givenName": "Wiliam Never",
"preferredAirport": "",
"travelerGroupId": null,
"externalId": null,
"externalId2": null,
"checkCCFieldForTraveler": false,
"notificationEmailOverride": null,
"paxType": 0,
"title": "Dr",
"middleName": null,
"dateOfBirth": "1980-01-15T00:00:00Z",
"phoneNumber": null,
"tsaInfo": null,
"source": "RequestEndPoint",
"allowBookingGuestTraveler": null,
"gdsTravelerProfileContainsFirstName": false,
"profilesFound": {
"Agency": null,
"Corporate": null,
"Traveler": null
},
"userId": "-2",
"firstName": "Wiliam",
"lastName": "Never"
},
"elements": []
我正在使用以下内容来获取我需要的信息
details = response.json()
checkIntent = details["itineraryIntent"]["userNodes"][0]["elements"]
if len(checkIntent) == 0:
print("No intent found, nothing is classified")
return False
else:
return True
这段代码在 80% 的情况下都有效。问题是有时我会
TypeError: 'NoneType' object is not subscriptable
我注意到,当尝试获取响应时,有时详细信息[“itineraryIntent”] [“userNodes”]本身为空。我尝试过使用 isIntances 和其他方法,但没有一个对我有用。
目前我已经在得到响应之前实现了睡眠 10 秒,但这不是一个好的解决方案,因为大多数情况下正确的响应会在 4-5 秒内出现。
我如何使其足够强大,以便我无需睡觉即可获得所需的结果。
尝试每个请求使用不同的代理(https://github.com/andigwandi/free-proxy)。如果您没有得到正确的回复 (
json
),请再次重新发送您的请求:
# Replace these values with your proxy information
proxy_host = 'your_proxy_host'
proxy_port = 'your_proxy_port'
# Define the proxy dictionary
proxy_dict = {
'http': f'http://{proxy_host}:{proxy_port}',
'https': f'https://{proxy_host}:{proxy_port}',
}
response = requests.get/post(url, proxies=proxy_dict_with_auth, ...)
details = response.json()
userNodes = details["itineraryIntent"]["userNodes"]
if not userNodes:
# change proxy
proxy_host = 'your_proxy_host'
proxy_port = 'your_proxy_port'
# Define the proxy dictionary
proxy_dict = {
'http': f'http://{proxy_host}:{proxy_port}',
'https': f'https://{proxy_host}:{proxy_port}',
}
response = requests.get/post(url, proxies=proxy_dict_with_auth, ...)
.....