我有一个内容为图像的URL,我希望使用python3请求下载它:
import requests
imageURL = "http://www.360mop.com/../upload/tw_header/2015063011211042.jpg"
response = requests.get(imageURL)
print(response)
<Response [400]>
有没有办法忽略&#39; ../'路径?或者我必须手动删除它?或者有更好的方法将图像形成URL?
答案 0 :(得分:0)
只需将相对路径替换为replace
即可:
import requests
root = 'http://www.360mop.com/'
other = "../upload/tw_header/2015063011211042.jpg".replace('../', '')
imageURL = root + other
response = requests.get(imageURL)
print response
#<Response [200]>
答案 1 :(得分:0)
这是我在Python3中解决问题的方法:
import os
from urllib.request import urlparse
def UrlResolve(url):
p = urlparse(url)
clear_url = p.scheme + "://" + p.netloc + os.path.normpath(p.path)
return clear_url