Update other_than.py

This commit is contained in:
HibiKier 2021-12-24 10:10:48 +08:00 committed by GitHub
parent 9b4cde819e
commit 71f860a500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,38 +5,44 @@
# @Email : youzyyz1384@qq.com # @Email : youzyyz1384@qq.com
# @File : other_than.py # @File : other_than.py
# @Software: PyCharm # @Software: PyCharm
import httpx from utils.http_utils import AsyncHttpx
from typing import Optional
from services.log import logger
import re import re
import json import json
__doc__='''爬虫实现国外疫情数据(找不到好接口)'''
__doc__ = """爬虫实现国外疫情数据(找不到好接口)"""
def intcomma(value): def intcomma(value) -> str:
""" """
数字格式化 数字格式化
""" """
orig = str(value) orig = str(value)
new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig) new = re.sub(r"^(-?\d+)(\d{3})", r"\g<1>,\g<2>", orig)
if orig == new: return new if orig == new else intcomma(new)
return new
else:
return intcomma(new)
async def get_other_data(place:str):
async def get_other_data(place: str) -> Optional[str]:
""" """
:param place: 地名 :param place: 地名
:return: 格式化字符串 :return: 格式化字符串
""" """
try: try:
html = httpx.get('https://news.ifeng.com/c/special/7uLj4F83Cqm').text.replace('\n', '').replace(' ', '') html = (
except: (await AsyncHttpx.get("https://news.ifeng.com/c/special/7uLj4F83Cqm"))
return .text.replace("\n", "")
find_data = re.compile(r'varallData=(.*?);</script>') .replace(" ", "")
sum = re.findall(find_data, html)[0] )
sum = json.loads(sum) except Exception as e:
other_country=sum['yiqing_v2']['dataList'][29]['child'] logger.error(f"疫情查询发生错误 {type(e)}{e}")
return None
find_data = re.compile(r"varallData=(.*?);</script>")
sum_ = re.findall(find_data, html)[0]
sum_ = json.loads(sum_)
other_country = sum_["yiqing_v2"]["dataList"][29]["child"]
for country in other_country: for country in other_country:
if place==country['name2']: if place == country["name2"]:
return ( return (
f"{place} 疫情数据:\n" f"{place} 疫情数据:\n"
"——————————————\n" "——————————————\n"
@ -46,12 +52,12 @@ async def get_other_data(place:str):
f"累计治愈:{intcomma(country['zhiyu'])}\n" f"累计治愈:{intcomma(country['zhiyu'])}\n"
f"死亡:{intcomma(country['siwang'])}\n" f"死亡:{intcomma(country['siwang'])}\n"
"——————————————" "——————————————"
#f"更新时间:{country['sys_publishDateTime']}" # f"更新时间:{country['sys_publishDateTime']}"
#时间无法精确到分钟网页用了js我暂时找不到 # 时间无法精确到分钟网页用了js我暂时找不到
) )
else: else:
for city in country['child']: for city in country["child"]:
if place==city['name3']: if place == city["name3"]:
return ( return (
f"{place} 疫情数据:\n" f"{place} 疫情数据:\n"
"——————————————\n" "——————————————\n"
@ -60,14 +66,5 @@ async def get_other_data(place:str):
f"累计治愈:{intcomma(city['zhiyu'])}\n" f"累计治愈:{intcomma(city['zhiyu'])}\n"
f"死亡:{intcomma(city['siwang'])}\n" f"死亡:{intcomma(city['siwang'])}\n"
"——————————————" "——————————————"
) )
return return None
if __name__ == '__main__':
a=get_other_data('英国')
print(a)
# print(get_other_data('美国'))
# print(get_other_data('印度'))
# print(get_other_data('伦敦'))