python 获取数据基本步骤

python 获取数据基本步骤,第1张

#加载模块
import requests
import re 
import json
import csv
import pandas as pd
 
#身份伪装,其实没必要
headers={
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'
        }
#请求地址
url='https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'
 
#发送请求
response=requests.get(url=url,headers=headers)
 
 
#数据解析
data_html=response.text
#【0】转换数据类型从list到str,强大的正则
json_str=re.findall('"component":\[(.*)\],',data_html)[0]
#转换字典
json_dict=json.loads(json_str)
caseList=json_dict['caseList']
 
for case in caseList:
    area=case['area']#
    confirmed=case['confirmed']#
    curConfirm=case['curConfirm']
    asymptomatic=case['asymptomatic']
    crued=case['crued']#
    died=case['died']#
    confirmedRelative=case['confirmedRelative']
    diedRelative=case['diedRelative']
    curedRelative=case['curedRelative']
    asymptomaticRelative=case['asymptomaticRelative']
    nativeRelative=case['nativeRelative']
    overseasInputRelative=case['overseasInputRelative']
 #打印检查  print(area,confirmed,curConfirm,confirmedRelative,nativeRelative,overseasInputRelative, asymptomatic,asymptomaticRelative,crued,curedRelative,died,diedRelative)
#写入表格    
with open('./data.csv',mode='a',encoding='utf-8',newline='')as f:
        csv_writer=csv.writer(f)
        csv_writer.writerow([area,confirmed,curConfirm,confirmedRelative,nativeRelative,overseasInputRelative,asymptomatic,asymptomaticRelative,crued,curedRelative,died,diedRelative])

审核了三天的文章,现在csdn的审核规则有点过分严格

最后的对比信息

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/563769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-03
下一篇 2022-04-03

发表评论

登录后才能评论

评论列表(0条)

保存