当前位置: 首页 > 面试题库 >

Python-通过urllib和python下载图片

沈树
2023-03-14
问题内容

我正在尝试制作一个Python脚本,该脚本可以下载网络漫画并将其放入我桌面上的文件夹中。我在这里发现了一些类似的程序,它们的功能相似,但是却完全不符合我的需要。我发现最相似的代码就在这里(http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images)。我尝试使用此代码:

>>> import urllib
>>> image = urllib.URLopener()
>>> image.retrieve("http://www.gunnerkrigg.com//comics/00000001.jpg","00000001.jpg")
('00000001.jpg', <httplib.HTTPMessage instance at 0x1457a80>)

然后,我在计算机上搜索了文件“ 00000001.jpg”,但我发现的只是它的缓存图片。我什至不确定它是否已将文件保存到我的计算机中。一旦我了解了如何下载文件,我想我就会处理其余的事情。本质上,只需要使用for循环并在‘00000000’。’jpg’处拆分字符串,然后将‘00000000’递增至最大数,我必须以某种方式确定该数字。关于最佳方法或正确下载文件的任何建议?

这是完成的脚本,它将文件保存到你选择的任何目录中。由于某种奇怪的原因,文件没有下载,而只是下载了。任何有关如何清理它的建议将不胜感激。我目前正在研究如何查找网站上存在的许多漫画,因此我只能获取最新的漫画,而不是在引发一定数量的异常后退出程序。

import urllib
import os

comicCounter=len(os.listdir('/file'))+1  # reads the number of files in the folder to start downloading at the next comic
errorCount=0

def download_comic(url,comicName):
    """
    download a comic in the form of

    url = http://www.example.com
    comicName = '00000000.jpg'
    """
    image=urllib.URLopener()
    image.retrieve(url,comicName)  # download comicName at URL

while comicCounter <= 1000:  # not the most elegant solution
    os.chdir('/file')  # set where files download to
        try:
        if comicCounter < 10:  # needed to break into 10^n segments because comic names are a set of zeros followed by a number
            comicNumber=str('0000000'+str(comicCounter))  # string containing the eight digit comic number
            comicName=str(comicNumber+".jpg")  # string containing the file name
            url=str("http://www.gunnerkrigg.com//comics/"+comicName)  # creates the URL for the comic
            comicCounter+=1  # increments the comic counter to go to the next comic, must be before the download in case the download raises an exception
            download_comic(url,comicName)  # uses the function defined above to download the comic
            print url
        if 10 <= comicCounter < 100:
            comicNumber=str('000000'+str(comicCounter))
            comicName=str(comicNumber+".jpg")
            url=str("http://www.gunnerkrigg.com//comics/"+comicName)
            comicCounter+=1
            download_comic(url,comicName)
            print url
        if 100 <= comicCounter < 1000:
            comicNumber=str('00000'+str(comicCounter))
            comicName=str(comicNumber+".jpg")
            url=str("http://www.gunnerkrigg.com//comics/"+comicName)
            comicCounter+=1
            download_comic(url,comicName)
            print url
        else:  # quit the program if any number outside this range shows up
            quit
    except IOError:  # urllib raises an IOError for a 404 error, when the comic doesn't exist
        errorCount+=1  # add one to the error count
        if errorCount>3:  # if more than three errors occur during downloading, quit the program
            break
        else:
            print str("comic"+ ' ' + str(comicCounter) + ' ' + "does not exist")  # otherwise say that the certain comic number doesn't exist
print "all comics are up to date"  # prints if all comics are downloaded

问题答案:

使用urllib.urlretrieve

import urllib
urllib.urlretrieve("http://www.gunnerkrigg.com//comics/00000001.jpg", "00000001.jpg")


 类似资料:
  • 问题内容: 我正在尝试使用urllib从ftp普查站点下载一个zip文件(“ tl_2008_01001_edges.zip”)。压缩文件以什么格式保存,如何保存? 我对Python还是很陌生,不了解urllib是如何工作的。 这是我的尝试: 如果我知道ftp文件夹列表(在这种情况下为县),是否可以使用glob函数浏览ftp站点列表? 谢谢。 问题答案: 使用的zip文件数据 和 目录列表。 要使

  • 问题内容: 我收到以下错误: 这是导致此错误的代码: 我使用的API要求我使用HTTPS。如何使其绕过验证? 问题答案: 如果只想绕过验证,则可以创建一个新的。默认情况下,新创建的上下文使用。 请注意第17.3.7.2.1节中的规定 直接调用构造函数时,默认值为。由于它不对另一个对等方进行身份验证,因此它可能是不安全的,尤其是在客户端模式下,在大多数情况下,你希望确保与之对话的服务器的真实性。因此

  • LinkedIn API有两个部分:1)从LinkedIns获取授权码(get请求)2)使用10秒左右的授权码从LinkedIn获取访问令牌(POST请求)。我的请求示例如下: 下面的第一部分,我可以在浏览器中成功完成,重定向到LinkedIn,并获取授权代码。https://www.linkedin.com/uas/oauth2/authorization?response_type=code

  • 本文向大家介绍Python通过IPython和ipdb,包括了Python通过IPython和ipdb的使用技巧和注意事项,需要的朋友参考一下 示例 如果安装了IPython(或Jupyter),则可以使用以下命令调用调试器: 到达后,代码将退出并打印: 显然,这意味着必须编辑代码。有一个更简单的方法: 如果引发了未捕获的异常,这将导致调试器被调用。

  • 问题内容: 何时会使用httplib和urllib? 有什么区别? 我想我已经准备好urllib使用httplib了,我计划制作一个需要发出http请求的应用程序,到目前为止,我只在python中使用了httplib.HTTPConnection来进行请求,并且阅读了有关urllib的信息,我也可以将其用于请求中,那么一个或另一个的好处是什么? 问题答案: urllib(尤其是urllib2)默认

  • 问题内容: 我有以下代码,可以轻松连接到FTP服务器并打开一个zip文件。我想将该文件下载到本地系统。怎么做? 问题答案: