Ich schreibe ein Python-Skript, das PDF-Datei lokal speichern wird entsprechend dem Format in URL angegeben. für z.Wie lade ich pdf-Datei über https mit Python
https://Hostname/saveReport/file_name.pdf #saves the content in PDF file.
Ich eröffne diese URL durch Python-Skript:
import webbrowser
webbrowser.open("https://Hostname/saveReport/file_name.pdf")
Die URL enthält viele Bilder und Text. Sobald diese URL geöffnet ist, möchte ich eine Datei im Pdf-Format mit Python-Skript speichern.
Das habe ich bisher gemacht. 1
Code:
import requests
url="https://Hostname/saveReport/file_name.pdf" #Note: It's https
r = requests.get(url, auth=('usrname', 'password'), verify=False)
file = open("file_name.pdf", 'w')
file.write(r.read())
file.close()
Code 2:
import urllib2
import ssl
url="https://Hostname/saveReport/file_name.pdf"
context = ssl._create_unverified_context()
response = urllib2.urlopen(url, context=context) #How should i pass authorization details here?
html = response.read()
In obigem Code ich erhalte: urllib2.HTTPError: HTTP-Fehler 401: Unauthorized
Wenn i-Code 2 verwenden, wie kann Ich gebe Autorisierungsdetails?
Sie arbeiten Sie eine Lösung mit 'webbrowser.open' wollen,' requests.get' oder 'urllib2. urlopen? –
@ Robᵩ - Ich habe oben Ansätze versucht. Also Anfragen oder urllib2 wird alles funktionieren. –