I was looking for a way to download pdf files in python, and I saw answers on other questions recommending the urllib module. I tried to download a pdf file using it, but when I try to open the downloaded file, a message shows up saying that the file cannot be opened.
What am I doing wrong? Also, the file automatically saves to the directory my python file is in. How do I change the location to which it gets saved? Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to download pdf files using Python? Ask Question. Asked 4 years, 6 months ago. Active 11 months ago.
Viewed 16k times. You can use requests for this task: stackoverflow. DavidZemens I won't call it a duplicate. The OP is concerned about his solution not working rather than finding a different one. Also cloudflare sites often restrict access based on user agent. If you open the file in a text editor you'll probably find html there instead of a pdf. So is there any way i can download files like that?? Show 4 more comments. Active Oldest Votes. Try this. It works. Fensa Saj Fensa Saj 1 1 silver badge 3 3 bronze badges.
Turns out this code does work. The PDF at the url in the code above happens to be corrupt. Check for the PDF file link in those links. Get a PDF file using the response object. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Last Updated : 13 Apr, Import libraries. URL from which pdfs to be downloaded.
Requests URL and get response object. Find all hyperlinks present on webpage. From all links check for pdf link and. Get response object for link. Write content in pdf file. Recommended Articles. Article Contributed By :.
Easy Normal Medium Hard Expert.
0コメント