代码脚本如下:
import urllib
import json
ctrip_header = {'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36',
'Host': 'flights.ctrip.com',
'Referer': 'http://flights.ctrip.com/booking/TAO-SJW-day-1.html?DDate1=2017-10-16'}
def search_flight(dcity, acity, ddate):
request_url = 'http://flights.ctrip.com/domesticsearch/search/SearchFirstRouteFlights?' \
+ 'DCity1=' + dcity \
+ '&ACity1=' + acity \
+ '&SearchType=S' \
+ '&DDate1=' + ddate
request = urllib.request(request_url, headers=ctrip_header)
response = urllib.urlopen(request)
return_json = response.read()
# print return_json
return_data = json.loads(return_json, encoding='gbk')
flight_list = return_data['fis']
# print flight_list
flight_nums = len(flight_list)
print('共有航班:', flight_nums, '趟')
for i in range(flight_nums):
airline = flight_list[i]['alc'] # 航空公司
flight_no = flight_list[i]['fn'] # 航班号
print('携程',airline, flight_no),
price_list = [each['p'] for each in flight_list[i]['scs'] if each['hotel'] is None] # 非飞宿产品价格
print (price_list)
if __name__ == '__main__':
search_flight('TAO', 'SJW', '2018-10-17')
运行时候出现报错:AttributeError: module 'urllib' has no attribute 'request'
求高手指教
慕设计2395807
小德是真的老实