Requests - get content-type/size without fetching the whole page/content

后端 未结 4 1188
悲&欢浪女
悲&欢浪女 2021-02-07 12:35

I have a simple website crawler, it works fine, but sometime it stuck because of large content such as ISO images, .exe files and other large stuff. Guessing content-type using

4条回答
  •  孤独总比滥情好
    2021-02-07 13:04

    Use requests.head() for this. It will not return the message body. You should use head method if you are interested only in the headers. Check this link for detail.

    h = requests.head(some_link)
    header = h.headers
    content_type = header.get('content-type')
    

提交回复
热议问题