python网络爬虫(第五章:实战7:jsonpath之数据解析)

实战1,拉勾网爬取城市列表

# # 实战7.爬取免费简历模板
# import requests
# from lxml import etree
# if __name__ == "__main__":
#     url = 'https://sc.chinaz.com/'
#     header = {
#         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
#     }
#     #获取页面源码数据,即获取响应数据
#     page_text = requests.get(url=url,headers=header).text
#
#     tree = etree.HTML(page_text)

import requests
from lxml import etree
import json
import jsonpath

if __name__ == "__main__":
    url = 'http://www.lagou.com/lbs/getAllCitySearchLabels.json'
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
    }
    #获取页面源码数据,即获取响应数据
    html = requests.get(url=url,headers=header).text

    print(html)

    #将JSON格式字符串转换成Python对象
    jsonobj = json.loads(html)
    all_city = []
    #解析城市名称
    city_list = jsonpath.jsonpath(jsonobj,'$..name')
    print(city_list,len(city_list))




版权声明:本文为qq_38633279原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。