我必须使用Python 3从以下网页中提取信息:http://www.homefinance.nl/english/international-interest-rates/libor/libor-interest-rates-gbp.asp
使用urllib.request下载似乎没问题,但令人惊讶的是,当我使用HTMLParser类解析html文件时,解析似乎停在元标记的中间,而没有给出任何理由。
这是我的代码:
import urllib.request
from html.parser import HTMLParser
def downloadLIBOR():
html_file = urllib.request.urlopen("http://www.homefinance.nl/english/international-interest-rates/libor/libor-interest-rates-gbp.asp")
return html_file
class tmpHTMLParser(HTMLParser):
def __init__(self):
self._libor = "0.81625 %"
self._stack = []
self._properStack = []
super().__init__()
def handle_starttag(self, tag, attrs):
print("starttag " + str(tag))
print(self.get_starttag_text())
self._stack.append(tag)
def handle_startendtag(self, tag, attrs):
print("startendtag")
def unknown_decl(self, data):
print("unknown_decl")
def handle_endtag(self, tag):
print("endtag " + str(tag))
self._stack.pop()
def _buildProperStack(webpage):
"""dev tool: return the stack leading to the libor rate libor into the webpage webpage."""
parser = tmpHTMLParser()
parser.feed(webpage)
if __name__ == "__main__":
webpage = downloadLIBOR()
print("download done")
html = str(webpage.read())
_buildProperStack(html)
exit(0)
答案 0 :(得分:2)
答案 1 :(得分:0)
不确定您实际尝试做什么,但使用BeautifulSoup解析HTML更好,更容易,更不容易出错。