python爬取各城市的油价

  1. 如何将汉字转换为拼音
>>> from xpinyin import Pinyin
>>> p = Pinyin()
>>> # default splitter is `-`
>>> p.get_pinyin(u"上海")
'shang-hai'
>>> # show tone marks
>>> p.get_pinyin(u"上海", show_tone_marks=True)
'shàng-hǎi'
>>> # remove splitter
>>> p.get_pinyin(u"上海", '')
'shanghai'
>>> # set splitter as whitespace
>>> p.get_pinyin(u"上海", ' ')
'shang hai'
>>> p.get_initial(u"上")
'S'
>>> p.get_initials(u"上海")
'S-H'
>>> p.get_initials(u"上海", u'')
'SH'
>>> p.get_initials(u"上海", u' ')
'S H'
  1. 获取城市的油价
import  urllib.request
import re
url = r'http://youjia.chemcp.com/shanndong/jinanshi.html'
res = urllib.request.urlopen(url)
html = res.read().decode('gb2312')
par = '(&nbsp;&nbsp;&nbsp;&nbsp;)(.*?)(<br />)'
web = re.search(par,html).group(2)
web = web.split('  ',1)[0]
cml = web.replace('<font color="red">',':')
cml = cml.replace('</font>','')
cml = cml.replace(',','\n')
cml = cml.replace(':','\n',1)
cml = cml.replace(',','\n')
print (cml)

参考链接:
http://www.javashuo.com/article/p-tdoeeudk-ct.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值