python - 如何使用美丽的汤提取特定部分中的维基百科链接?
问题描述
我正在尝试提取维基百科页面https://en.wikipedia.org/wiki/Privacy_law的另见部分中的 URL 。
我尝试了以下代码:
url_req = "https://en.wikipedia.org/wiki/Privacy_law"
response = requests.get(url=url_req,)
soup = BeautifulSoup(response.content, 'html.parser')
snippet = soup.find_all('h2')
for headline in snippet:
if re.findall('see.{0,5}also',str(headline),re.IGNORECASE):
links = headline.findall('a')
print(links)
我能够找到正确的标题,但无法访问 URL。他们是在一个<div>
特定的之后<h2>
。如何获取这些 URL?
解决方案
你可以使用维基百科库:
https://wikipedia.readthedocs.io/en/latest/
这是图书馆的一个例子
wikipedia.search(query, results=10, suggestion=False)¶
推荐阅读
- css - Antora 补充 ui 从 doc.css 中的 doc 中删除 max-width
- python - 是否可以使用 DataFrame.query() 来判断列是否存在?
- javascript - 使用 TLE 线在地面轨迹图上显示轨道
- reactjs - CRA 开发服务器停止查看文件更改
- python - 了解 Python 在使用 VS-Code 时如何从子目录中导入模块
- python - 查找 lxml 的命名空间 URI
- flutter - 使用动作参数 Flutter 测试 Redux-Saga
- list - SwiftUI - List / ForEach 结合 NavigationLink 和 isActive 无法正常工作
- python-3.x - 当活动用户为root时Python获取用户名
- wordpress - 如何在不停机的情况下在 wordpress 中处理新主题