【发布时间】:2019-11-27 22:58:37
【问题描述】:
我必须将“字符串”类型的硬编码路径连接到一个 URL 以获得一个 URL 的结果。
url(不以“/”结尾)+“/path/to/file/”=new_url
我尝试使用 URL 连接进行连接,也尝试使用简单字符串 concat,但结果不是可以访问的 URL。 (不是URL地址无效)
mirror_url = "http://amazonlinux.us-east-
2.amazonaws.com/2/core/latest/x86_64/mirror.list"
response = requests.get(mirror_url)
contents_in_url = response.content
## returns a URL as shown below but of string type which cannot be
##concatenated to another string type which could be requested as a valid
##URL.
'http://amazonlinux.us-east- 2.amazonaws.com/2/core/2.0/x86_64/8cf736cd3252ada92b21e91b8c2a324d05b12ad6ca293a14a6ab7a82326aec43'
path_to_add_to_url = "/repodata/primary.sqlite.gz"
final_url = contents_in_url + path_to_add_to_url
期望的结果:
不省略该文件的任何路径。
final_url = "http://amazonlinux.us-west-2.amazonaws.com/2/core/2.0/x86_64/8cf736cd3252ada92b21e91b8c2a324d05b12ad6ca293a14a6ab7a82326aec43/repodata/primary.sqlite.gz"
【问题讨论】:
-
那么你得到的结果是什么?你看过stackoverflow.com/questions/1793261/…吗?
-
@intotecho 是的,但就我而言,我必须坚持使用“请求”库,不能使用“urllib”。
标签: python-3.x string python-2.7 url python-requests