python - Python - 使用 handle_starttag 和 handle_endtag 处理 htmlparser 数据
问题描述
我正在尝试handle_data
根据给定的限制来控制handle_starttag
,endtag
但是,我无法实现这一点。
我还使用了设置为True
并False
实现此目的的标志,但没有对数据和标签进行控制。
这是我的代码:
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.htmlList = []
self.flag = False
def handle_starttag(self,tag,attrs):
if tag.lower() in ['a','title'] and tag.lower() != 'script':
self.flag = True
def handle_data(self, data):
if self.flag == True:
words = data.split()
for i in words:
if i.isalpha() == True:
self.htmlList.append(i)
return (self.htmlList)
解决方案
推荐阅读
- php - 由于 e^ 无法解决 php 数学中的数学公式
- javascript - 即使我仅在“getStaticProps()”中使用 fs 代码,也找不到模块“fs/promises”-下一个 js
- reactjs - 在 React js 中的 http://hostname/ 之后元标记不起作用
- reactjs - 使用 x 和 y 坐标创建图表
- android - Android Capacitor 应用程序仅设置标头引发网络错误
- python - 绘制树形图时python浮点除以零
- c# - 如何在 C# 中查找类、命名空间和项目文件夹
- api - 使用 nextjs 应用程序中的 axios 将文件上传到 graphql api 服务器时我错在哪里?
- python - 是什么导致 pandas pivot_table 失败?
- ios - 用新的 App 替换 Apple Store 上的现有 App