How to check the url is either web page link or file link in python

前端 未结 2 1169
轻奢々
轻奢々 2021-01-06 09:57

Suppose i have links as follows:

    http://example.com/index.html
    http://example.com/stack.zip
    http://example.com/setup.exe
    http://example.com/n         


        
2条回答
  •  难免孤独
    2021-01-06 10:10

    import urllib
    mytest = urllib.urlopen('http://www.sec.gov')
    mytest.headers.items()
    
    ('content-length', '20833'), ('expires', 'Sun, 02 Feb 2014 19:36:12 GMT'), ('server', 'SEC'), ('connection', 'close'), ('cache-control', 'max-age=0'), ('date', 'Sun, 02 Feb 2014 19:36:12 GMT'), ('content-type', 'text/html')]
    

    mytest.headers.items() is a list of tuples, you can see in my example that the last item in the list describes the content

    I am not sure if the length varies so you could iterate through it to find the one that has 'content-type' in it.

提交回复
热议问题