-6

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?

user1255968
  • 9
  • 1
  • 2
  • It is unclear: do you want to *open* it (a difficult thing to do), or do you want to *download* it? In the case of the latter, `urllib` might work. – xxmbabanexx Mar 23 '13 at 23:12
  • 1
    [`urlretrieve()`](http://docs.python.org/3.0/library/urllib.request.html#urllib.request.urlretrieve) – millimoose Mar 23 '13 at 23:12
  • we are here to help you with problems with your code, not to create code for you. – Wouter J Mar 23 '13 at 23:19
  • Here is a similar question/answer http://stackoverflow.com/a/9751490/322909 – John Mar 23 '13 at 23:35
  • possible duplicate of [Opening pdf urls with pyPdf](http://stackoverflow.com/questions/9751197/opening-pdf-urls-with-pypdf) – Jaime Mar 24 '13 at 00:03

1 Answers1

8

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()
A. Rodas
  • 20,171
  • 8
  • 62
  • 72