如何使用相对路径提取href的绝对URL?

时间:2019-06-22 15:29:59

标签: python-3.x web-scraping

我正在尝试从此link中提取下载链接。

以下是该链接的页面来源(在Google Chrome中查看):

enter image description here

当我指向页面源上的../matlab/licensing.pdf时,会出现一个链接https://www.mathworks.com/help/pdf_doc/matlab/licensing.pdf

我检查了../matlab/licensing.pdf,但是链接没有出现在右侧。因此,我无法在Python中使用regrex提取此链接。

请帮助我从页面源中提取此链接。

1 个答案:

答案 0 :(得分:1)

尝试使用urllib.parse.urljoin

示例:

import urllib.parse

base = r"https://www.mathworks.com/help/pdf_doc/install/index.html"
link_in_html = r"../matlab/licensing.pdf"

result = urllib.parse.urljoin(base, link_in_html)

print(result)