推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Leon1s
V2EX  ›  Python

萌新请教 Python 爬虫附件下载问题

  •  
  •   Leon1s · Jun 15, 2020 · 2262 views
    This topic created in 2160 days ago, the information mentioned may be changed or developed.
    有一个一直困扰的问题,爬虫过程中已经获取到所有附件的链接和文件名(不包含后缀名)到数据库,现在提取数据库里的信息进行附件下载。

    问题是:
    1.有些文件的后缀名可能不在 url 中,或者头信息中。没有办法获取到文件后缀名,下载的附件没有文件类型。
    2.有些文件的真实后缀名跟 url 中的不一样。

    请问各位大佬有什么好的办法解决这个痛点吗,谢谢。
    8 replies    2020-06-30 10:03:33 +08:00
    j0hnj
        1
    j0hnj  
       Jun 15, 2020
    关键词:guess content type
    Leon1s
        2
    Leon1s  
    OP
       Jun 15, 2020
    @j0hnj 谢谢回复,可以麻烦详细说说吗。首先 guess 是什么,刚才没百度到。另外 content_type 应该是我说的第 1 点,可能头信息没有包含在内。
    silencefly
        3
    silencefly  
       Jun 15, 2020 via iPhone
    根据文件最开始的字节匹配文件类型
    zdnyp
        4
    zdnyp  
       Jun 15, 2020
    1.后缀名在 response 的 header 里 2.放个例子
    xiri
        5
    xiri  
       Jun 15, 2020
    @Leon1s guess:猜测
    python 又相应的包可以自动检测(猜测)文件类型
    xiri
        6
    xiri  
       Jun 15, 2020
    @xiri 又->有
    xiri
        7
    xiri  
       Jun 15, 2020
    @Leon1s mimetypes.guess_type()
    daiqiangbudainiu
        8
    daiqiangbudainiu  
       Jun 30, 2020
    magic number,应该有相应的库来做这个事
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2594 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 545ms · UTC 16:01 · PVG 00:01 · LAX 09:01 · JFK 12:01
    ♥ Do have faith in what you're doing.