import requests import re import json import random class Province: def get_province(self): url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince" headers = {"Content-Type": "application/x-www-form-urlencoded"} rep = requests.get(url=url,headers=headers) str_list = re.findall('<string>(.+?)</string>',rep.text) return (str_list) def get_city(self): """ 根据省份获取城市响应 :return: """ url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity" province_random = random.choice(self.get_province()) data ={ "byProvinceName":province_random } headers = {"Content-Type": "application/x-www-form-urlencoded"} rep = requests.post(url=url,data=data,headers=headers) # print(rep.text) city_c = re.compile('<string>(\W.+?)\(\d.+?\)</string>',re.A) city_list = city_c.findall(rep.text) print(city_list) if __name__=="__main__": provice=Province() # provice.get_province() provice.get_city()
运行结果