如何在特定的英文维基百科文章中找到所有英文维基百科链接

2024-06-26 00:16:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个图形项目,我想找到一种有效的方法,从一篇特定的英文维基百科文章中获取到其他英文维基百科文章的所有链接

目前,我正在使用bs4和Python,但我对bs4了解不多

以下是我现在拥有的:

##### Imports #####
from bs4 import BeautifulSoup
from bs4.dammit import EncodingDetector
import requests
 

##### Functions #####
parser = 'html.parser'
resp = requests.get("https://en.wikipedia.org/wiki/Influenza")
http_encoding = resp.encoding if 'charset' in resp.headers.get('content-type','').lower() else None
html_encoding = EncodingDetector.find_declared_encoding(resp.content, is_html=True)
encoding = html_encoding or http_encoding
soup = BeautifulSoup(resp.content, parser, from_encoding=encoding)
 
for link in soup.find_all('a', href=True):
     print(link['href'])

问题是我得到了很多不需要的链接(非英语链接或非文章链接)。我认为我对HTML的了解不够,无法解决这个问题,我不想简单地过滤上面find_all()调用中提供给我的每个链接,因为这样做效率很低

如有任何建议,将不胜感激。 提前谢谢


Tags: fromimportparserget链接html文章content