鱼C论坛

 找回密码
 立即注册
查看: 3495|回复: 27

[技术交流] 使用爬虫,获得疫情数据(已修正)

[复制链接]
发表于 2021-1-14 04:17:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Daniel_Zhang 于 2021-1-14 19:38 编辑

终于修正好了,应该没有bug了吧。。。

  1. import re, ssl
  2. import requests
  3. import pandas as pd

  4. city_or_country = []        # 所有国家/地区的疫情信息
  5. all_data = []        # 暂时没有使用,请等待更新

  6. def open_url(url):
  7. # encoding: utf-8

  8.     headers = {
  9.     'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
  10.     'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  11.     'cookie': "_ntes_nuid=48631ba26359e3892a6d0bf6cf52b544; _ntes_nnid=73e9c0b05b63f1d4ac3467f7d86e9b88,1610523997314; _antanalysis_s_id=1610524397238; _ntes_newsapp_install=false; hb_MA-975A-DBD9471124CE_source=www.google.com"
  12.    
  13.     }
  14.     f = open('testing_new.txt','w')
  15.     ssl._create_default_https_context = ssl._create_unverified_context
  16.     html = requests.get(url,headers=headers).text # 获取url内容
  17.     return html

  18. def get_html(url):
  19.     html = open_url(url)
  20.     return html

  21. '''
  22. def get_country_or_city_code(url):
  23.     html = get_html(url)
  24.     m = r'"name".+?"id".+?,'
  25.     result = re.findall(m, html)
  26.     for each in range(len(result)):
  27.         final = (result[each].split('"'))
  28.         city_or_country.append([final[3],final[7]])
  29.     return city_or_country
  30. '''

  31. def get_data_all(url):
  32.     html = get_html(url)
  33.     index1 = html.index('areaTree') # 获取所需数据部分的其实位置
  34.     html = html[index1:] # 去除 api 开头的不需要使用到的数据
  35.     m = r'{"today".+?"name".+?"id"'        # 正则表达式获取所有国家的数据
  36.     result = re.findall(m, html)
  37.    
  38.     for each in range(len(result)):
  39.         final = (result[each].split('"'))
  40.         country_data_set = []
  41.         for every in range(len(final)):
  42.             while (':' or ',' or '}' or '{') in final[every]:        # 去除掉不必要的符号
  43.                 if ':' in final[every]:
  44.                     new_string = ''
  45.                     new_string = final[every]
  46.                     new_string = new_string.split(':')[-1]
  47.                     final[every] = new_string
  48.                 if ',' in final[every]:
  49.                     new_string = ''
  50.                     new_string = final[every]
  51.                     new_string = new_string.split(',')[0]
  52.                     final[every] = new_string  
  53.                 if '}' in final[every]:
  54.                     new_string = ''
  55.                     new_string = final[every]
  56.                     new_string = new_string.split('}')[0]
  57.                     final[every] = new_string   
  58.                 if '{' in final[every]:
  59.                     new_string = ''
  60.                     new_string = final[every]
  61.                     new_string = new_string.split('{')[-1]
  62.                     final[every] = new_string
  63.             if final[every].isdigit() or final[every] == 'null':        # 获取治愈,死亡等数据,如果是数据或者是 null,则写入到 list 中,否则舍弃
  64.                 country_data_set.append(final[every])           
  65.         all_data.append(country_data_set)

  66.     n = r'"name".+?"id".+?"children"'   # 写入国家/地区名称,编号,数据更新时间
  67.     result_2 = re.findall(n, html)
  68.     for each in range(len(result_2)):
  69.         string1 = result_2[each]
  70.         new_list = string1.split('"')
  71.         all_data[each].insert(0,new_list[3])  
  72.         all_data[each].insert(1,new_list[7])
  73.         all_data[each].insert(2,new_list[11])
  74.     return all_data

  75. def save_as_txt(all_data, title):
  76.     f = open('covid-19_data_set.txt','w')
  77.     for each in title:
  78.         each = str(each).center(25,' ')        # 对齐一下,增加美观
  79.         f.write(each)
  80.     f.write('\n')
  81.     for each_one in all_data:
  82.         for each in each_one:
  83.             each = str(each).center(25,' ')        # 对齐一下,增加美观
  84.             f.write(each)
  85.         f.write('\n')
  86.     f.close()

  87. def save_as_csv(all_data, title):
  88.     csv = pd.DataFrame(columns=title,data=all_data)
  89.     csv.to_csv('csv.csv',index=False,encoding='utf_8_sig')

  90. if __name__ == "__main__":
  91.     url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=322104795402'
  92.     '''get_country_or_city_code(url)'''
  93.     all_data = get_data_all(url)
  94.   
  95.     title = ['name','id','lastUpdateTime','today_confirm','today_suspect','today_heal','today_dead','today_severe','today_storeConfirm','total_confirm','total_suspect','total_heal','total_dead','total_severe','total_input','newConfirm','newDead','newHeal']        # 文件表头
  96.     enter = input('please enter the number here: 1. save as txt file 2. save as csv file\n')
  97.     if enter.isdigit():
  98.         if int(enter) == 1:
  99.             save_as_txt(all_data, title)
  100.         elif int(enter) == 2:
  101.             save_as_csv(all_data, title)
复制代码


谢谢各位的观看

稍后会补齐 get_country_or_city_code 函数部分的功能。截至目前,代码可以使用了,get_country_or_city_code 函数只是为了实现按照地区进行搜索所需要的结果,稍后会更新出来~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-14 08:28:12 | 显示全部楼层

回帖奖励 +2 鱼币

。试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-14 09:00:16 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-14 10:49:34 | 显示全部楼层
感谢分享!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-14 10:51:35 | 显示全部楼层

回帖奖励 +2 鱼币

谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-14 11:21:17 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-14 11:22:07 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-14 11:34:11 | 显示全部楼层

回帖奖励 +2 鱼币

中奖了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-14 13:54:16 | 显示全部楼层
excel乱码了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-14 15:03:04 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-14 16:34:30 | 显示全部楼层

啥乱码?######这种?如果是的话,那就把那一行拉开就能显示了。

如果不是,请仔细描述一下问题,谢谢!

我这里运行的结果是这样的:

截屏2021-01-14 16.33.38.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-14 16:55:11 | 显示全部楼层

啊哈哈,不好意思哈,我刚查了一下百度,因为我是mac系统,所以在运行的时候不会出现这个问题,在 win 里面才会有这个问题

解决方式:
在输出成csv格式的时候,将原来的 csv.to_csv('csv.csv') 代码改成如下
csv.to_csv('csv.csv',index=False,encoding='utf_8_sig')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-14 17:00:24 | 显示全部楼层
大佬膜拜
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-14 17:02:53 | 显示全部楼层

回帖奖励 +2 鱼币

再来一次
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-15 15:54:29 | 显示全部楼层
我想要鱼币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-15 15:55:04 | 显示全部楼层

回帖奖励 +2 鱼币

再来一次
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-15 17:13:54 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-15 19:20:20 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-15 21:45:38 | 显示全部楼层

回帖奖励 +2 鱼币

太赞啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-17 11:03:16 | 显示全部楼层

回帖奖励 +2 鱼币

感谢分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 20:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表