from mechanize import Browser
br = Browser()
br.open('http://somewebpage')
html = br.response().readlines()
for line in html:
print line
当在HTML文件中打印一行时,我试图找到一种方法,只显示每个HTML元素的内容,而不是格式本身。如果它发现'<a href="等等。例如">some text</a>',它只会打印'some text', '<b>hello</b>'打印'hello',等等。该怎么做呢?
我已经成功地在Python 3.1中使用了Eloff的答案[非常感谢!]。
我升级到Python 3.2.3,并遇到了错误。
解决方案,这里提供感谢响应器Thomas K,是插入super().__init__()到以下代码:
def __init__(self):
self.reset()
self.fed = []
... 为了让它看起来像这样:
def __init__(self):
super().__init__()
self.reset()
self.fed = []
... 它适用于Python 3.2.3。
再次感谢Thomas K的修复和Eloff提供的原始代码!
有一个简单的方法:
def remove_html_markup(s):
tag = False
quote = False
out = ""
for c in s:
if c == '<' and not quote:
tag = True
elif c == '>' and not quote:
tag = False
elif (c == '"' or c == "'") and tag:
quote = not quote
elif not tag:
out = out + c
return out
这里解释了这个想法:http://youtu.be/2tu9LTDujbw
你可以在这里看到它的工作:http://youtu.be/HPkNPcYed9M?t=35s
PS -如果你对这个课程(关于使用python进行智能调试)感兴趣,我给你一个链接:http://www.udacity.com/overview/Course/cs259/CourseRev/1。它是免费的!
欢迎你!:)