python - 在 Python 中使用 HTMLParser 提取超链接 URL 和内容
问题描述
我正在尝试将我的维基百科监视列表转换为可读的降价格式文档。为此,我使用 Python 的html
内置模块从他们的网站解析本地保存的 html。到目前为止,我制作了一个脚本来提取所有相关链接,但我未能在输出中包含它们的内容(引理名称)。还尝试handle_starttag
为数据实现另一个,但我无法正确过滤以仅包含<a>
监视列表项的标签。
这是我到目前为止的进展
import html
from html.parser import HTMLParser
with open(r'data/Edit watchlist - Wikipedia.html', "r") as f:
page = f.read()
class Parse(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
def handle_starttag(self, tag, attrs):
if tag == "a":
for name, link in attrs:
if (name == "href" and
"Talk:" not in link and
"Special:" not in link and
"Help:" not in link and
"Wikipedia:" not in link and
"index.php" not in link and
link.startswith("http")):
print (link)
p = Parse()
p.feed(page)
解决方案
推荐阅读
- python - 如何单击下一步按钮以使用 selenium python 从所有页面中抓取数据?
- javascript - 如何在 carousel-control-next 的“上方”放置一个按钮
- python - 如何将一个数字数组链接到另一个数字数组?
- transform - 如何使用 gdal 通过一个命令将 png tile 的投影从 epsg:4326 转换为 epsg:3857
- python - 在 Django 应用程序中使用 gmail smtp 发送电子邮件时出错
- java - 为什么函数 checkAB 不能正常工作?
- python - 在哪里可以找到函数的 kwargs/args 文档
- c++ - 使用 g++ 编译 olcPixelGameEngine 时出错
- node.js - 从 NodeJS、NLP 中的文本中提取人名
- node.js - 没有 --experimental-modules 标志,使用 import 的 Node.js 不起作用