def load_list(cls, local_path=None): if local_path is None: url = "https://kyfw.12306.cn/otn/resources/js/query/train_list.js" response = webrequest.get(url) text = response.text else: with open(local_path, "rt", encoding="utf-8") as f: text = f.read() # Remove var keyword assert text.startswith("var ") text = text[4:] # Execute the JS as Python code variables = {} exec(text, variables) # Load the train data date_map = {} for train_date_str, type_map in variables["train_list"].items(): train_date = timeconverter.str_to_date(train_date_str) train_map = {} for train_type, train_list in type_map.items(): for train_obj in train_list: train_id = train_obj["train_no"] train_name = train_obj["station_train_code"] train_name = re.match("(.+)\(.*-.*\)", train_name).group(1) train_map[train_name] = train_id date_map[train_date] = train_map cls.__data__ = date_map
def logout(self): webrequest.get("https://kyfw.12306.cn/otn/login/loginOut", cookies=self.cookies, allow_redirects=False) logger.debug("Successfully logged out")