How to download PDF using Python

Shashankk
Nov 29, 2019

--

Photo by Nicolas Picard on Unsplash

Python is really cool language and most important I love the web scraping concept of python. so basically web scraping can be referred to as gathering information from sources such as websites and using it for project

I have written a program that downloads a pdf from a specified link for example I have to use a dummy site that does not really exist to download a pdf

import socket

import requests

url= “http://xyz.com/sample.pdf"

res = requests.get(url)

print(res)

f = open(“sample.pdf”,”wb”)

f.write(res.content)

f.close()

In this example, it copies all content of the pdf file to a new file called smaple.pdf and stores it in the current directory.if pdf is not on site it will show error 404 i.e file not found

response error 404

I would like to share more experiments that I have done with web scraping

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shashankk
Shashankk

Written by Shashankk

0 Followers

Engineering student who loves coding

No responses yet

Write a response