python - Write PDF file from URL using urllib2 -


i'm trying save dynamic pdf file generated web server using python's module urllib2. use following code data server , write data file in order store pdf in local disk.:

import urllib2 import cookielib  theurl = 'https://myweb.com/?pdf&var1=1' cj = cookielib.cookiejar() opener = urllib2.build_opener(urllib2.httpcookieprocessor(cj)) opener.addheaders.append(('cookie', cookie)) request = urllib2.request(theurl)  print("... sending http %s" % theurl) f = opener.open(request) data = f.read() f.close() opener.close()  file = open('report.pdf', "w") file.write(data) file.close() 

this code runs written pdf file not recognized adobe reader. if request manually using firefox, have no problems receive file , can visualize withouut problems. comparing received http headers (firefox , urrlib) difference http header field called "transfer-encoding = chunked". field received in firefox seems not received when urllib request. suggestion?

try changing,

file = open('report.pdf', "w") 

to

file = open('report.pdf', "wb") 

the 'b' indicates write in binary mode. writing binary file in ascii/text mode.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -