我有一些包含Unicode内容的Html文件。我需要提取内容。在
这是我的代码,它不起作用
import os
from urllib import urlopen
from bs4 import BeautifulSoup
url= "http://www.dinakaran.com/News_Detail.asp?Nid=76468"
raw = urlopen(url).read()
soup = BeautifulSoup(raw,'lxml')
texts = soup.findAll(text=True)
def contains_unicode(text):
try:
str(text)
except:
return True
return False
result = ''.join((text for text in texts if contains_unicode(text)))
输出:
^{pr2}$我只需要Unicode内容
不会删除HTML标记
有什么建议吗?在
如果只想列出包含非ASCII值的字符串,请使用:
请注意,该页上的某些文本同时包含ASCII和ASCII范围之外的代码点。在
快速演示:
^{pr2}$最后一行包含
\xa9
,©版权符号。您可以通过匹配非拉丁语(编码为latin1
而不是ASCII
)来进一步过滤掉这些内容,或者可以使用正则表达式测试特定Unicode范围(如泰米尔语)。在如果需要拒绝仅包含一个可打印ASCII字符的行,则需要使用正则表达式:
^{3}$如果输入字符串中没有ASCII字母,则只返回
True
:相关问题 更多 >
编程相关推荐