我有一个html页面:
<div class="theater">
<div class="desc" id="theater_16109207495969942346">
<h2 class="name"><a href="/movies?near=pune&tid=df8f66de0a592b4a" id="link_1_theater_16109207495969942346">Esquare Victory Camp</a></h2>
<div class="info">site no 2429,general thimayya road, camp contonment,oppositekayani bakery, Pune - 020 2613 2975
<a class="fl" href="" target="_top"></a>
</div>
</div>
<div class="showtimes">
<div class="show_left">
<div class="movie">
<div class="name"><a href="/movies?near=pune&mid=1cdcf90092189400">Hawaa Hawaai</a>
</div><span class="info">Drama - Hindi</span>
<div class="times"><span style="color:#666"><span style="padding:0 "></span>
<!-- -->10:30am</span><span style="color:#666"><span style="padding:0 "> &nbsp</span>
<!-- -->3:45</span><span style="color:#666"><span style="padding:0 "> &nbsp</span>
<!-- -->6:00</span><span style="color:"><span style="padding:0 "> &nbsp</span>
<!-- -->8:30pm</span>
</div>
</div>
</div>
<div class="show_right">
<div class="movie">
<div class="name"><a href="/movies?near=pune&mid=6b59ad39004d895b">The Amazing Spider Man 2</a>
</div><span class="info">Action/Adventure/Thriller - English - <a class="fl" href="/url?q=http://www.youtube.com/watch%3Fv%3DSCjCk59PIzw&sa=X&oi=movies&ii=0&usg=AFQjCNGpVM5U04h0acABA7eApb6EIO4Ejw">Trailer</a></span>
<div class="times"><span style="color:#666"><span style="padding:0 "></span>
<!-- -->1:00</span><span style="color:"><span style="padding:0 "> &nbsp</span>
<!-- -->10:45pm</span>
</div>
</div>
</div>
<p class="clear"></p>
</div>
</div>
我们可以看到我们在很多地方都有&nbsp
。还有许多其他unicode字符。我想提取这个页面的内容。
我在做的是:
def removeNonAscii(s): return "".join(i for i in s if ord(i)<128)
myName = soup.findAll("div", {"class" : "theater"})
for x in myName:
xt = str(x)
print removeNonAscii(xt)
print "<br>"
结果:
Esquare Victory Camp
site no 2429,general thimayya road, camp contonment,oppositekayani bakery, Pune - 020 2613 2975
Hawaa Hawaai
Drama - Hindi
10:30am  3:45  6:00  8:30pm
The Amazing Spider Man 2
Action/Adventure/Thriller - English - Trailer
1:00  10:45pm
除 
外,一切看起来都不错。我尝试更换&amp; nbsp,并搜索其他解决方案,但仍然没有解决方案。我认为 
没有;
会产生问题。如何删除 
?
答案 0 :(得分:8)
根据您要删除不间断空间的处理阶段,它可以非常简单。例如,当您处理您提供的HTML片段时,您只需删除字符串&#34;&amp; nbsp&#34;来自文本元素:
s = """your HTML"""
soup = BeautifulSoup(s)
texts = soup.find_all(text=True)
for t in texts:
newtext = t.replace(" ", "")
t.replace_with(newtext)
答案 1 :(得分:3)
lxml.html 可能是一个更适合您的库,它会将“&amp; nbsp ”和其他HTML标记替换为正确的字符。
import lxml.html
import lxml.html.clean
html = """your HTML"""
doc = lxml.html.fromstring(html)
cleaner = lxml.html.clean.Cleaner(style=True)
doc = cleaner.clean_html(doc)
text = doc.text_content()