python中turtle库画冠状病毒_Python实现实时数据采集新型冠状病毒数据实例

Python实时数据采集-新型冠状病毒

疫情数据时间为:2020.2.1

202002040841222.jpg

项目相关截图:

全国数据展示

202002040841223.png

国内数据展示

202002040841224.png

国外数据展示

202002040841225.png

查看指定区域详细数据

202002040841236.png

源代码,注意安装所需模块(例如 pip install 模块名)

import requests

import re

from bs4 import BeautifulSoup

from time import sleep

import json

from prettytable import ALL

from prettytable import PrettyTable

hubei = {}

guangdong = {}

zhejiang = {}

beijing = {}

shanghai = {}

hunan = {}

anhui = {}

chongqing = {}

sichuan = {}

shandong = {}

guangxi = {}

fujian = {}

jiangsu = {}

henan = {}

hainan = {}

tianjin = {}

jiangxi = {}

shanxi1 = {} # 陕西

guizhou = {}

liaoning = {}

xianggang = {}

heilongjiang = {}

aomen = {}

xinjiang = {}

gansu = {}

yunnan = {}

taiwan = {}

shanxi2 = {} # 山西

jilin = {}

hebei = {}

ningxia = {}

neimenggu = {}

qinghai = {} # none

xizang = {} # none

provinces_idx = [hubei, guangdong, zhejiang, chongqing, hunan, anhui, beijing,

shanghai, henan, guangxi, shandong, jiangxi, jiangsu, sichuan,

liaoning, fujian, heilongjiang, hainan, tianjin, hebei, shanxi2,

yunnan, xianggang, shanxi1, guizhou, jilin, gansu, taiwan,

xinjiang, ningxia, aomen, neimenggu, qinghai, xizang]

map = {

'湖北':0, '广东':1, '浙江':2, '北京':3, '上海':4, '湖南':5, '安徽':6, '重庆':7,

'四川':8, '山东':9, '广西':10, '福建':11, '江苏':12, '河南':13, '海南':14,

'天津':15, '江西':16, '陕西':17, '贵州':18, '辽宁':19, '香港':20, '黑龙江':21,

'澳门':22, '新疆':23, '甘肃':24, '云南':25, '台湾':26, '山西':27, '吉林':28,

'河北':29, '宁夏':30, '内蒙古':31, '青海':32, '西藏':33

}

def getTime(text):

TitleTime = str(text)

TitleTime = re.findall('(.*?)', TitleTime)

return TitleTime[0]

def getAllCountry(text):

AllCountry = str(text)

AllCountry = AllCountry.replace("[

", "")

AllCountry = AllCountry.replace("", "")

AllCountry = re.sub("", "", AllCountry)

AllCountry = AllCountry.replace("

]", "")

AllCountry = AllCountry.replace("", "")

AllCountry = re.sub("", "", AllCountry)

AllCountry = re.sub("

", "", AllCountry)

AllCountry = re.sub("

", "", AllCountry)

return AllCountry

def query(province):

table = PrettyTable(['地区', '确诊', '死亡', '治愈'])

for (k, v) in province.items():

name = k

table.add_row([name, v[0] if v[0] != 0 else '-', v[1] if v[1] != 0 else '-', v[2] if v[2] != 0 else '-'])

if len(province.keys()) != 0:

print(table)

else:

print("暂无")

def getInfo(text):

text = str(text)

text = re.sub("

", "", text)

text = re.sub("

", "", text)

return text

def is_json(json_str):

try:

json.loads(json_str)

except ValueError:

return False

return True

def ff(str, num):

return str[:num] + str[num+1:]

def main():

url = "https://3g.dxy.cn/newh5/view/pneumonia"

try:

headers = {}

headers['user-agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36' #http头大小写不敏感

headers['accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'

headers['Connection'] = 'keep-alive'

headers['Upgrade-Insecure-Requests'] = '1'

r = requests.get(url, headers=headers)

r.raise_for_status()

r.encoding = r.apparent_encoding

soup = BeautifulSoup(r.text,'lxml')

table = PrettyTable(['地区', '确诊', '死亡', '治愈'])

table.hrules = ALL

#### 截至时间

# TitleTime = getTime(soup.select('.title___2d1_B'))

print()

# print(" ",TitleTime + "\n")

while True:

r = requests.get("https://service-f9fjwngp-1252021671.bj.apigw.tencentcs.com/release/pneumonia")

json_str = json.loads(r.text)

if json_str['error'] == 0:

break

print("==================================全国数据==================================")

print()

print(" 确诊 " + str(json_str['data']['statistics']['confirmedCount']) + " 例"

+ " " + "疑似 " + str(json_str['data']['statistics']['suspectedCount']) + " 例"

+ " " + "死亡" + str(json_str['data']['statistics']['deadCount']) + " 例"

+ " " + "治愈" + str(json_str['data']['statistics']['curedCount']) + " 例\n")

print("==================================相关情况==================================")

print()

print("传染源:" + json_str['data']['statistics']['infectSource'])

print("病毒:" + json_str['data']['statistics']['virus'])

print("传播途径:" + json_str['data']['statistics']['passWay'])

print(json_str['data']['statistics']['remark1'])

print(json_str['data']['statistics']['remark2'] + "\n")

print("==================================国内情况==================================")

print()

json_provinces = re.findall("{\"provinceName\":(.*?)]}", str(soup))

idx = 0

for province in json_provinces:

if is_json(province):

pass

else:

province = "{\"provinceName\":" + province + "]}"

province = json.loads(province)

province_name = province['provinceShortName'] if province['provinceShortName'] != 0 else '-'

confirmed = province['confirmedCount'] if province['confirmedCount'] != 0 else '-'

suspected = province['suspectedCount'] if province['suspectedCount'] != 0 else '-'

cured = province['curedCount'] if province['curedCount'] != 0 else '-'

dead = province['deadCount'] if province['deadCount'] != 0 else '-'

table.add_row([province_name, confirmed, dead, cured])

map[province_name] = idx

idx = idx + 1

for city in province['cities']:

provinces_idx[map[province_name]][city['cityName']] = [city['confirmedCount'], city['deadCount'], city['curedCount']]

print(table)

print()

print("==================================国外情况==================================")

print()

json_provinces = str(re.findall("\"id\":949(.*?)]}", str(soup)))

json_provinces = json_provinces[:1] + "{\"id\":949" + json_provinces[2:]

json_provinces = json_provinces[:len(json_provinces) - 2] + json_provinces[len(json_provinces) - 1:]

provinces = json.loads(json_provinces)

table = PrettyTable(['地区', '确诊', '死亡', '治愈'])

for province in provinces:

confirmed = province['confirmedCount'] if province['confirmedCount'] != 0 else '-'

dead = province['deadCount'] if province['deadCount'] != 0 else '-'

cured = province['curedCount'] if province['curedCount'] != 0 else '-'

table.add_row([province['provinceName'], confirmed, dead, cured])

print(table)

print()

print("==================================最新消息==================================")

print()

idx = 0

for news in json_str['data']['timeline']:

if idx == 5:

break

print(news['pubDateStr'] + " " + news['title'])

idx = idx + 1

print()

key = input("请输入您想查询详细信息的省份,例如 湖北\n")

print()

if key in map.keys():

query(provinces_idx[map[key]])

else:

print("暂无相关信息")

print("\n欢迎提出各种意见")

except:

print("连接失败")

if __name__ == '__main__':

main()

sleep(30)

以上就是Python实时数据采集-新型冠状病毒的详细内容,大家出门要做好安全措施,感谢对脚本之家的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用Pythonturtle可以轻松地绘制各种图形,包括线条、圆形、矩形、多边形等等。通过turtle提供的函数和方法,可以控制海龟的移动和绘制,实现各种有趣的效果。例如,可以使用turtle绘制一个简单的正方形: import turtle turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.done() 这段代码会绘制一个边长为100的正方形。其turtle.forward()函数表示向前移动一定距离,turtle.right()函数表示向右旋转一定角度。最后,调用turtle.done()函数表示绘制完成,等待用户关闭窗口。 除了绘制基本图形,turtle还支持更高级的绘图功能,例如填充颜色、设置线条宽度、绘制文本等等。可以通过查阅turtle的文档,了解更多的绘图方法和技巧。 ### 回答2: Pythonturtle是一个基于Windows图形界面的函数,可以使用户在屏幕上绘制图形。使用turtle图可以增加程序的趣味性,同时也能够让程序员更好地理解数学基础知识,比如坐标系、角度、线性代数等。 在开始使用turtle之前,我们需要引入turtle模块。下面是一个最基本的turtle绘图代码实例: ``` import turtle turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.done() ``` 上述代码实现了在屏幕上绘制了一个边长为100的正方形。其turtle.forward(n)表示向右前进n个单位,turtle.right(n)表示向右旋转n度。turtle.done()用于保持图像窗口不关闭,直到用户手动关闭窗口为止。 turtle绘图的基本操作包括:前进、后退、旋转、抬笔、落笔、改变颜色、改变线宽等。此外,我们也可以借助turtle绘制各种图形,如圆形、三角形、正多边形、心形等。 下面是一个使用turtle绘制五芒星的实现代码: ``` import turtle turtle.penup() turtle.goto(0,0) turtle.pendown() turtle.color('red','yellow') turtle.begin_fill() for i in range(5): turtle.forward(200) turtle.right(144) turtle.end_fill() turtle.done() ``` 上述代码实现了在屏幕上绘制了一个红黄相间的五芒星。其turtle.penup()和turtle.pendown()分别用于抬笔和落笔。turtle.color('red','yellow')用于设置线条颜色为红色,填充颜色为黄色。turtle.begin_fill()和turtle.end_fill()用于标识需要填充颜色的区域。 绘制图形时,我们可以使用代码循环进行绘制,这可以大幅度加速绘制的过程。下面是一个绘制螺旋线的实现代码: ``` import turtle step = 2 angle = 10 turtle.speed(0) for i in range(360): turtle.forward(step) turtle.right(angle) step += 0.05 angle += 0.5 turtle.done() ``` 上述代码实现了在屏幕上绘制了一个螺旋线。其,step表示每次前进的距离,angle表示每次右转的角度。turtle.speed(0)用于加速绘制速度。通过实验调节angle和step的大小,我们可以绘制出各种不同的曲线形状。 总之,使用turtle图可以让程序更具趣味性和可视化。无论是初学Python的新手还是有经验的程序员,都可以通过绘制各种形状来提高自己的编程水平。 ### 回答3: Pythonturtle是一个非常有趣的绘图工具,它简单易用但又足够强大,可以用于绘制各种图形。在Python使用turtle,可以让我们探索计算机图形的奥妙,同时也可以增强我们的创造力和编程能力。 要使用turtle编写程序,首先需要导入turtle。可以使用以下代码导入: ```python import turtle ``` 在导入后,可以创建一个布和一个笔来开始绘。使用以下代码创建一个布: ```python turtle.setup(width, height, startx, starty) ``` 该函数接受四个参数,分别是布的宽度、高度、起始坐标。需要注意的是,如果这四个参数都不提供,将使用缺省值。 创建笔后,就可以使用turtle提供的各种绘图函数来绘制各种形状和图形。一些常用的函数如下: ```python turtle.forward(distance) turtle.backward(distance) turtle.right(angle) turtle.left(angle) turtle.goto(x, y) turtle.circle(radius) ``` 这些函数可以让我们实现各种基本形状,例如直线、曲线、圆等。 为了实现更多复杂的绘图,我们可以使用循环和条件语句,来使代码更加流畅和可扩展。通常,我们会使用while循环或for循环来生成一系列图形,如以下示例: ```python for i in range(4): turtle.forward(100) turtle.right(90) ``` 这段代码会绘制一个正方形,使用四次循环来实现。 总的来说,Pythonturtle提供了一个有趣、易用、功能强大的绘图工具,可以让用户通过代码来实现各种图形,并增强用户的编程能力和想像力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值