mingche 发表于 2018-8-7 06:41:34

Python,精炼

-- coding: UTF-8 --
  import pycurl
  c = pycurl.Curl()
  c.setopt(pycurl.URL, 'http://file.allitebooks.com/20171129/Beginning%20PowerApps.pdf')
  import StringIO#这个用到里面的write函数
  b = StringIO.StringIO()
  c.setopt(pycurl.WRITEFUNCTION, b.write) #把StringIO的写函数注册到pycurl的WRITEFUNCTION中,即pycurl所有获取的内容都写入到StringIO中,如果没有这一句,pycurl就会把所有的内容在默认的输出器中输出,也就是将返回的内容定向到回调函数b.write,且传参给这个方法。
  c.setopt(pycurl.TIMEOUT, 3000)
  c.perform()
  print b.getvalue()
  str = b.getvalue()
  with open('D:\pythonscripts\test', 'wb+') as fb:
  fb.write(str)
  执行的结果是:文件D:\pythonscripts\test生成,之前该目录没有这个文件。而且是pdf格式的文件,用Adobe Acrobat 7.0 Professional 可以直接将其打开。
页: [1]
查看完整版本: Python,精炼