我找不到如何获取网站的完整地址: 例如,我得到“/wiki/Main_Page”而不是“https://en.wikipedia.org/wiki/Main_Page”。 我不能简单地将 url 添加到链接,因为它会给出:“https://en.wikipedia.org/wiki/WKIK/wiki/Main_Page”,这是不正确的。 我的目标是使其适用于任何网站,因此我正在寻找通用的解决方案。
这是代码:
from bs4 import BeautifulSoup
import requests
url ="https://en.wikipedia.org/wiki/WKIK"
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data)
for link in soup.find_all('a', href=True):
print "Found the URL:", link['href']
这是它返回的部分内容:
>Found the URL: /wiki/WKIK_(AM)
>Found the URL: /wiki/WKIK-FM
>Found the URL: /wiki/File:Disambig_gray.svg
>Found the URL: /wiki/Help:Disambiguation
>Found the URL: //en.wikipedia.org/w/index.php?
>title=Special:WhatLinksHere/WKIK&namespace=0
最佳答案
此处的其他答案可能会遇到某些相对 URL 的问题,例如包含句点的 URL (../page
)。
Python 的 requests
库有 a function called urljoin获取完整的 URL:
requests.compat.urljoin(currentPage, link)
因此,如果您访问 https://en.wikipedia.org/wiki/WKIK
,并且页面上有一个 href
为 的链接/wiki/Main_Page
,该函数将返回https://en.wikipedia.org/wiki/Main_Page
。
关于python - 如何使用 BeautifulSoup 获取完整网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44746021/