http消息不是可呼叫的python2到python3下载脚本

发布于 2025-01-24 07:13:42 字数 1045 浏览 2 评论 0原文

我正在尝试移植到python 3这个脚本:

import re 

from os.path import basename
import os
from urllib.parse import urlparse,urlsplit
from urllib.request import urlopen,Request
import urllib 
def url2name(url):
    return basename(urlsplit(url)[2])

def download(url, out_path="."):
    localName = url2name(url)
    req = Request(url)
    r = urlopen(req)
    if r.info().has_key('Content-Disposition'):
        # If the response has Content-Disposition, we take file name from it
        localName = r.info()['Content-Disposition'].split('filename=')[1]
        if localName[0] == '"' or localName[0] == "'":
            localName = localName[1:-1]
    elif r.url != url: 
        # if we were redirected, the real file name we take from the final URL
        localName = url2name(r.url)

    localName = os.path.join(out_path, localName)
    f = open(localName, 'wb')
    f.write(r.read())
    f.close()

但是我有一个:

'httpmessage'对象不可callable

r.info()似乎有问题

如何在Python 3中获取标题信息?

i'am trying to port to python 3 this script :

import re 

from os.path import basename
import os
from urllib.parse import urlparse,urlsplit
from urllib.request import urlopen,Request
import urllib 
def url2name(url):
    return basename(urlsplit(url)[2])

def download(url, out_path="."):
    localName = url2name(url)
    req = Request(url)
    r = urlopen(req)
    if r.info().has_key('Content-Disposition'):
        # If the response has Content-Disposition, we take file name from it
        localName = r.info()['Content-Disposition'].split('filename=')[1]
        if localName[0] == '"' or localName[0] == "'":
            localName = localName[1:-1]
    elif r.url != url: 
        # if we were redirected, the real file name we take from the final URL
        localName = url2name(r.url)

    localName = os.path.join(out_path, localName)
    f = open(localName, 'wb')
    f.write(r.read())
    f.close()

but i have a :

'HTTPMessage' object is not callable

r.info() seems to have problems

how to get the header info in python 3 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沐歌 2025-01-31 07:13:42

尝试一下,您应该使用上下文经理:

def download(url, out_path="."):
    localName = url2name(url)
    req = Request(url)
    with urlopen(req) as f:
        content_disp = f.getheader('Content-Disposition')
        if content_disp:
            # If the response has Content-Disposition, we take file name from it
            localName = content_disp.split('filename=')[1]
            if localName[0] == '"' or localName[0] == "'":
                localName = localName[1:-1]
        elif f.url != url:
            # if we were redirected, the real file name we take from the final URL
            localName = url2name(f.url)

        localName = os.path.join(out_path, localName)

        with open(localName, 'wb') as fp:
            fp.write(f.read())

Try with this, you should use context managers:

def download(url, out_path="."):
    localName = url2name(url)
    req = Request(url)
    with urlopen(req) as f:
        content_disp = f.getheader('Content-Disposition')
        if content_disp:
            # If the response has Content-Disposition, we take file name from it
            localName = content_disp.split('filename=')[1]
            if localName[0] == '"' or localName[0] == "'":
                localName = localName[1:-1]
        elif f.url != url:
            # if we were redirected, the real file name we take from the final URL
            localName = url2name(f.url)

        localName = os.path.join(out_path, localName)

        with open(localName, 'wb') as fp:
            fp.write(f.read())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文