import requests
import jsonpath
import json


def test_name():
    # 获取拉勾网城市json字符串
    url = 'http://www.lagou.com/lbs/getAllCitySearchLabels.json'
    headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"}
    response = requests.get(url, headers=headers)
    html_str = response.content.decode()

    # 把json格式字符串转换成python对象
    jsonobj = json.loads(html_str)
    # 从根节点开始，获取所有key为name的值
    citylist = jsonpath.jsonpath(jsonobj, '$..name')

    # 写入文件
    with open('03_city_name.txt', 'w', encoding='utf-8') as f:
        content = json.dumps(citylist, ensure_ascii=False)
        f.write(content)


if __name__ == '__main__':
    test_name()
