text: Q:
  
  How to download pdf files in python?
  
  I need to download something like
  str = 'http://query.nytimes.com/mem/archive-free/pdf?res=9A00EEDE1431E13BBC4850DFBF66838A649FDE'
  url = urllib2.urlopen(str)
  file = open('test.pdf', 'w')
  file.write(url.read())
  file.close()
  
  It just creates a wrong pdf.
  how do I write that into file?
  
  A:
  
  You can use the pattern module, which is built on top of urllib2 and has a higher level of abstraction.
  from pattern.web import URL
  
  url = URL('http://query.nytimes.com/mem/archive-free/pdf?res=9A00EEDE1431E13BBC4850DFBF66838A649FDE')
  f = open('nytimes.pdf', 'wb')
  f.write(url.download(cached=False))
  f.close()
  
  