Skip to main content
 首页 » 编程设计

python之如何在 Python 中使用 referer 和 href 查找 url

2025年05月04日26qq78292959

假设我有

window_location = 'http://stackoverflow.com/questions/ask' 
href = '/users/48465/jader-dias' 

我要获取

link = 'http://stackoverflow.com/users/48465/jader-dias' 

我如何在 Python 中执行此操作?

它必须像在浏览器中一样工作

请您参考如下方法:

>>> import urlparse 
>>> urlparse.urljoin('http://stackoverflow.com/questions/ask', 
...                  '/users/48465/jader-dias') 
'http://stackoverflow.com/users/48465/jader-dias' 

来自 urlparse.urljoin 的文档页面:

urlparse.urljoin(base, url[, allow_fragments])

Construct a full (“absolute”) URL by combining a “base URL” (base) with another URL (url). Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL.

If url is an absolute URL (that is, starting with // or scheme://), the url‘s host name and/or scheme will be present in the result.