MarinaF
MarinaF

Reputation: 75

Web Scraping problem with downloading file - link automatically downloads PDF file but I can't save it correctly

I have a link that when pressed automatically downloads a PDF file

The link looks something like this:

""https://u14350383.ct.sendgrid.net/ls/click?upn=pmcnQ2-2FzMlkei0-2F............"

There is no PDF extension at the end.

My code is very simple:

reponse = requests.get(url)
if not os.path.isfile(filePath):
    fp = open(filePath, 'wb')
    fp.write(reponse.content)(decode=True))
    fp.close()

I tried writing it to a PDF file with response.content and response.text but each time I get an "object not callable" error. When I print the text/content of the response I get the PDF file, how can I actually save it?

Upvotes: 0

Views: 220

Answers (1)

Sheshanandh
Sheshanandh

Reputation: 56

fp.write(reponse.content)(decode=True)) is wrong and don't try to save bytes content as string. result will be strange

Edit

   reponse = requests.get(url)
   if not os.path.isfile(filePath):
      fp = open(filePath, 'wb')
      fp.write(reponse.content)
      fp.close()

Upvotes: 2

Related Questions