最近写了一个12306查票的Python程序,在调试过程中遇到name not defined的bug,这个程序在Python3.6版本下是可以正常运行的,今天在Python2.7环境下运行一直提示这个,希望得到一个解决办法
程序如下:
macbookpro:bin miyamoto$ ./python -VPython 2.7.10复制代码
#!/usr/bin/env python# -*- coding:utf-8 -*-import requestsimport json from PIL import Imageimport getpassimport rerequests.packages.urllib3.disable_warnings(InsecureRequestWarning)from urllib3.exceptions import InsecureRequestWarningif __name__ == '__main__': url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version' # 根据查看源码查出来的 response = requests.get(url, verify=False) print response.text stations = re.findall('([a-z]+)\|([\u4e00-\u9fa5]+\|[A-Z]+)', response.text) # 用正则表达式 来获取车站的拼音和大小写字母的代号信息 print stations stszm = re.findall('[\u4e00-\u9fa5]+\|[A-Z]+', response.text) # 用正则表达式 来获取车站的拼音和大小写字母的代号信息 # start_input = input("请输入始发站:\n") # stopt_input = input("请输入终点站:\n") # tripDate = input("请输入日志[YYYY-mm-dd]:\n") start_input="北京西" stopt_input="石家庄" tripDate="2018-09-30" for startAndStopStation in stations: if startAndStopStation[1] == str(start_input): startStation = start_input elif startAndStopStation[1] == str(stopt_input): stopStation = stopt_input for stationsSuoxie in stszm: if str(stationsSuoxie).split('|')[0] == start_input: startStationSuoxie = str(stationsSuoxie).split('|')[1] print startStationSuoxie elif str(stationsSuoxie).split('|')[0] == stopt_input: stopStationSuoxie = str(stationsSuoxie).split('|')[1] print stopStationSuoxie # print str(stationsSuoxie).split('|')[0] adult = "https://kyfw.12306.cn/otn/leftTicket/queryA?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT" %(tripDate,startStationSuoxie,stopStationSuoxie) # ticketlist=requests.request('get',adult).text print adult复制代码
结果最后调试一直报错,语法方面和逻辑方面没有错误。求解!
Traceback (most recent call last): File "/Users/miyamoto/Documents/PycharmProject/12306.py", line 191, inadult = "https://kyfw.12306.cn/otn/leftTicket/queryA?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT" %(tripDate,startStationSuoxie,stopStationSuoxie)NameError: name 'startStationSuoxie' is not defined复制代码
另外再付一个bug
#!/usr/bin/env python# -*- coding:utf-8 -*-import json from PIL import Imageimport requestsimport getpassimport refrom urllib3.exceptions import InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning)if __name__ == '__main__': url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version' # 根据查看源码查出来的 # response=urllib2.urlopen(url) # print response.read() response = requests.get(url, verify=False) stations = re.findall("([a-z]+)\|([\u4e00-\u9fa5]+))", response.text) print stations复制代码
上面这个代码运行出来后显示为空字典,求解。
/Users/miyamoto/Documents/PycharmProject/python3/bin/python /Users/miyamoto/Documents/PycharmProject/12306_new.py[]复制代码
另外在python2.7.10下单独使用re.findall()进行正则匹配respone.text,没有任何信息;在python3.6以上的环境下进行匹配就会有信息展示,下面是python3.7的结果(中文前面的车站缩写我没有添加):
['北京北', '北京东', '北京', '北京南', '北京西', '广州南', '重庆北']复制代码