python获取qq在线人数
程序员文章站
2022-07-13 08:33:30
...
"""
功能:获取qq在线人数
作者:Mac
时间:2020.6.10
"""
import requests
import json
import csv
# 请求头信息
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0",
}
# 目标链接
target = "http://www.off0.com/list"
# 发起请求
response = requests.get(url=target, headers=headers)
# 将json数据转换成字典
load_data = json.loads(response.content)
# 将数据写入cvs文件,newline用于取消隔行写入,utf-8-sig解决中文乱码
with open("qqdata.cvs", "w", newline="", encoding="utf-8-sig") as cf:
writer = csv.writer(cf)
# 写入首行
writer.writerow(["时间", "在线总数", "变化"])
for data in load_data:
writer.writerow([data["time"], data["total"], data["wave"]])
print("写好了")