动态获取关联接口的城市信息

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()

运行结果

猜你喜欢

转载自www.cnblogs.com/caowenyue/p/13383396.html
今日推荐