使用dateutil解析日期和时间,LXML提取URL

import nltk
from dateutil import parser
from dateutil import tz
import datetime
from lxml import html
import lxml.html.clean
from bs4 import BeautifulSoup


# 使用dateutil解析日期和时间
print(parser.parse('Thu Sep 25 10:36:28 2010'))
# 2010-09-25 10:36:28
print(parser.parse('Thursday, 25. September 2010 10:36AM'))
# 2010-09-25 10:36:00
print(parser.parse('9/25/2010 10:36:28'))
# 2010-09-25 10:36:28
print(parser.parse('9/25/2010'))
# 2010-09-25 00:00:00
print(parser.parse('2010-09-25T10:36:28Z'))
# 2010-09-25 10:36:28+00:00
# day first
print(parser.parse('25/9/2010', dayfirst=True))
# 2010-09-25 00:00:00
print(parser.parse('10-9-25'))
# 2025-10-09 00:00:00
print(parser.parse('10-9-25', yearfirst=True))
# 2010-09-25 00:00:00
print(parser.parse('9/25/2010 at about 10:36AM', fuzzy=True))
# 2010-09-25 10:36:00

# 时区的查找和转换
tz.tzutc()
print(tz.tzutc().utcoffset(datetime.datetime.utcnow()))
print(datetime.timedelta(0))
print(tz.gettz('US/Pacific'))

# 使用lxml从HTML中提取URL
# lxml是一个结合了C库libxml2和libxslt的Python库
doc = html.fromstring('Hello <a href="www.baidu.com">world</a>')
print(doc)
# <Element p at 0x212f696b228>
links = list(doc.iterlinks())
len(links)
(el, attr, link, pos) = links[0]
print(el, attr, link, pos)
# <Element a at 0x175be942ea8> href /world 0

doc.make_links_absolute('http://hello')
abslinks = list(doc.iterlinks())
(el, attr, link, pos) = abslinks[0]
print(link)
# http://hello/www.baidu.com
links = list(html.iterlinks('Hello <a href="/world">world</a>'))
print(links[0][2])

# 清理和剥离HTML
print(lxml.html.clean.clean_html('<html><head></head><body onload=loadfunc()>my text</body></html>'))
# <div><body>my text</body></div>
# 使用Beautiful Soup转换HTML实体
print(BeautifulSoup('&lt;', features="lxml").string)
print(BeautifulSoup('&amp;', features="lxml").string)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值