【问题标题】:Python request can't get image from urlPython请求无法从url获取图像
【发布时间】:2015-07-31 04:03:47
【问题描述】:

我有一个包含图片的 URL,我希望使用 python3 请求下载它:

import requests
imageURL = "http://www.360mop.com/../upload/tw_header/2015063011211042.jpg"
response = requests.get(imageURL)

print(response)
<Response [400]>

有没有办法忽略“../”路径?或者我必须手动删除它?还是有更好的方法从 URL 中获取图像?

【问题讨论】:

标签: python image python-3.x request


【解决方案1】:

只需用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]>

【讨论】:

    【解决方案2】:

    这是我在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多