#coding=utf-8
import re
from urllib2 import urlopen
webpage = urlopen('http://www.baidu.com') #获取百度页面的信息
text = webpage.read() #读取为文本
tmp = text.decode('utf8') #对原文本进行utf8转码, 此处要跟代码的编码格式一致
pat = '<title>(.*)?([\u4e00-\u9fa5]*)?</title>' #对中文进行匹配
re.escape(pat) #对匹配模式中需要转义的符号进行转义
pat = re.compile(pat) #compile一下
m = re.search(pat,tmp)
title = m.group(1)
print title
webpage.close()
在unicode中, 中文是\u4e00-\u9fa5
本博客展示了如何使用Python获取网页信息,利用正则表达式匹配中文标题,并进行UTF-8转码。适用于网站内容抓取和网页分析。
3048

被折叠的 条评论
为什么被折叠?



