json数据爬虫。requests实现

Posted 安迪9468

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json数据爬虫。requests实现相关的知识,希望对你有一定的参考价值。

import json
import requests

# url = "https://www.mamalaile.cn/mamalailegw/page/waiterList.jsp"
url = "https://www.mamalaile.cn/momcome-app/waiter/api_queryUserWaiterInfo.do"

headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘
}

response = requests.get(url, headers=headers)
data_str = response.content.decode()
data_dict = json.loads(data_str)
print(type(data_dict))
print(len(data_dict[‘data‘]))
print(data_dict[‘data‘][0][‘name‘])
i = 0
for item in data_dict[‘data‘]:
    i += 1
    print(str(i))
    print(‘姓名:%s‘%item[‘name‘])
    print(‘年龄:%s‘%item[‘age‘])
    print(‘籍贯:%s‘%item[‘place‘])
    print(‘属相:%s‘%item[‘animals‘])

  

以上是关于json数据爬虫。requests实现的主要内容,如果未能解决你的问题,请参考以下文章

Python2爬虫代码之获取金融品种行情数据

数据获取--爬虫-3(Request库)

python爬虫中涉及json数据的处理

python爬虫5--requests请求库

Python爬虫之json动态数据抓取

爬虫之JSON案例