代码脚本如下:import urllibimport jsonctrip_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=' + ddaterequest = urllib.request(request_url, headers=ctrip_header)response = urllib.urlopen(request)return_json = response.read()# print return_jsonreturn_data = json.loads(return_json, encoding='gbk')flight_list = return_data['fis']# print flight_listflight_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'求高手指教
2 回答
小德是真的老实
TA贡献6条经验 获得超2个赞
python3的把urllib中的很多东西合并了,像urllib.urlopen这种改成
urllib.request.urlopen()
你可以去看下python3的urllib库
添加回答
举报
0/150
提交
取消