用python做数据采集过程中,海外网站的时间处理无疑是非常麻烦的,那么各种语言的时间如何转换utc时间,这里安利两个强大的时间解析库
1、dtxg库(自动解析外文时间日期的库)
dtxg库 是 dateutil 的强大扩展,可以解析多种国家语言,根据时区参数返回标准utc时间
dtxg模块为 标准的dateutil模块,在python中提供。
安装:
dtxg可以使用pip从pypi安装:
pip install dtxg
直接写个demo测试一下效果
from dtxg.parser import parse, UtcTime, country_tz
# test2
a = parse(" Thứ ba, 18/6/2019 | 4:41:27 Chiều", language='西班牙语', tzinfo='UTC', country=None, fuzzy=True)
print(a)
# test2
utctime = UtcTime(language='西班牙语', country='中国')
a = utctime.parse(" Thứ ba, 18/6/2019 | 4:41:27 Chiều", fuzzy=True)
print(a)
print(country_tz.country_tz) 查看各国时区(即country_tz的有效参数)
print(country_tz.country_language) 查看各国语言(即language的有效参数)
pytz.all_timezones 查看所有地区时区,(即tzinfo的有效参数)
lanauge 传入时间字符串的语言种类
tzinfo 时区
country 国家,用于tzinfo为空时获取时区信息
fuzzy 模糊匹配
返回utc时间
没有解析到时间会抛出异常
可识别 "2019-06-10T08:00:00-05:00" "UTC", "GMT", "Z", "z"
优先识别字符串中的时区,其次识别tzinfo传入的时区,如果两者都没有,则以传入country的首都时区返回utc时间,
如果都为空,则返回原时间
文章转自:猿派的博客_CSDN博客-git,MySQL,python领域博主
项目地址 :
GitHub - yangsongbai1/dtxghttps://github.com/yangsongbai1/dtxg
2、dateparser
dateparser是一个强大的python开源库,对于多语言多格式的时间做出了通用的解析
github: GitHub - scrapinghub/dateparser: python parser for human readable dates
文档:Installation — DateParser 1.1.2 documentation
下载直接pip即可
pip install dateparser
使用起来也非常简单,直接调用对应方法即可
不过目前dateparser针对一些不规则时间还是存在解析错误的情况,具体自测吧