Python 如何将“href”中的“短链接”转换为实际URL?

Python 如何将“href”中的“短链接”转换为实际URL?,python,url,web-scraping,web-crawler,uri,Python,Url,Web Scraping,Web Crawler,Uri,比方说我正在抓取一个网页,我把它的所有链接都刮掉了。在python中,如何转换以下链接: Catalog.php Products.aspx Contact.html https://example.com/Catalog.php https://example.com/Products.aspx https://example.com/Contact.html 连接到以下实际链接: Catalog.php Products.aspx Contact.html https://exampl

比方说我正在抓取一个网页,我把它的所有链接都刮掉了。在python中,如何转换以下链接:

Catalog.php
Products.aspx
Contact.html
https://example.com/Catalog.php
https://example.com/Products.aspx
https://example.com/Contact.html
连接到以下实际链接:

Catalog.php
Products.aspx
Contact.html
https://example.com/Catalog.php
https://example.com/Products.aspx
https://example.com/Contact.html
我用DuckGo的力量搜索了堆栈溢出上的所有地方。也许这个问题有重复的地方,但我不知道该如何表达这个问题。

假设你有一个基本路径

import urllib.parse
urllib.parse.urljoin("https://example.com", "/Catalog.php")
您可以从urllib使用urljoin方法

通过将一个“基本URL”基与另一个URL相结合,构造一个完整的“绝对”URL。非正式地说,它使用基本URL的组件,特别是寻址方案、网络位置和部分路径,来提供相对URL中缺少的组件

导入urllib.parse 基本路径=https://example.com/ 相对路径=/Catalog.php new\u url=urllib.parse.urljoinbase\u路径,相对路径 你得到

>>> https://example.com/Catalog.php