Downloading a picture via urllib and python

后端 未结 18 1593
离开以前
离开以前 2020-11-22 10:35

So I\'m trying to make a Python script that downloads webcomics and puts them in a folder on my desktop. I\'ve found a few similar programs on here that do something simila

18条回答
  •  故里飘歌
    2020-11-22 11:11

    Maybe you need 'User-Agent':

    import urllib2
    opener = urllib2.build_opener()
    opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36')]
    response = opener.open('http://google.com')
    htmlData = response.read()
    f = open('file.txt','w')
    f.write(htmlData )
    f.close()
    

提交回复
热议问题