【发布时间】:2016-09-23 21:13:54
【问题描述】:
我在 Python 3.5.1 上运行 Beautifulsoup4。
我目前有这个代码:
from bs4 import BeautifulSoup
import html5lib
class LinkFinder(BeautifulSoup):
def __init__(self):
super().__init__()
def handle_starttag(self, name, attrs):
print(name)
当我通过以下代码实例化类时:
findmylink = LinkFinder() 当我使用以下代码加载我的 html 时 findmylink.feed("""<html><head><title>my name is good</title></head><body>hello world</body></html>""",'html5lib').
我的控制台出现以下错误:
'NoneType' object is not callable
我实际上希望复制以下示例代码(在我的例子中,我希望使用 Beautifulsoup 而不是 html.parser)
from html.parser import HTMLParser
class LinkFinder(HTMLParser):
def __init__(self):
super().__init__()
def handle_starttag(self, tag, attrs):
print(tag)
当我通过以下代码重新实例化该类时:findmylink = LinkFinder() 并且当我使用以下代码 findmylink.feed("""<html><head><title>my name is good</title></head><body>hello world</body></html>""") 加载我的 html 时,我得到以下输出:
html
head
title
body
这是所需的输出。
【问题讨论】:
标签: python html python-3.x web-scraping beautifulsoup