如何在Python中使用html.parser从特定的HTML链接中提取数据



我有我需要的东西,下面有一个非常愚蠢的解析器。我想创建一个方法,该方法将 HTML 页面的 URL (例如:http://www.dictionary.com/browse/example(作为参数,并使用此解析器向我显示它遇到的所有数据。我不需要有人给我解决方案。但是,如能提供建议,将不胜感激。谢谢。

from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
    def handle_data(self, data):
        print("Encountered some data  :", data)
parser = MyHTMLParser()
parser.feed('<html><head><title>Test</title></head>'
            '<body><h1>Parse me!</h1></body></html>')
这就是

我最终从URL中提取数据的方式,在本例中为 http://python.org/。

from html.parser import HTMLParser
from urllib.request import urlopen
class MyHTMLParser(HTMLParser):
    def handle_data(self, data):
        print("Encountered some data  :", data)
parser = MyHTMLParser()
html = urlopen('http://python.org/')
thing = html.read()
parser.feed(thing.decode("utf-8"))

最新更新