How to get pdf filename with Python requests?

后端 未结 5 761
渐次进展
渐次进展 2020-12-09 08:16

I\'m using the Python requests lib to get a PDF file from the web. This works fine, but I now also want the original filename. If I go to a PDF file in Firefox and click

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 08:26

    It is specified in an http header content-disposition. So to extract the name you would do:

    import re
    d = r.headers['content-disposition']
    fname = re.findall("filename=(.+)", d)[0]
    

    Name extracted from the string via regular expression (re module).

提交回复
热议问题