Richieste HTTP per importare file dal web

Importazione di dati in Python - livello intermedio

Hugo Bowne-Anderson

Data Scientist at DataCamp

URL

  • Uniform/Universal Resource Locator
  • Riferimenti a risorse web
  • Focus: indirizzi web
  • Componenti:
    • Identificatore di protocollo - http:
    • Nome risorsa - datacamp.com
  • Specificano un indirizzo web univoco
Importazione di dati in Python - livello intermedio

HTTP

  • HyperText Transfer Protocol
  • Base della comunicazione dati sul web
  • HTTPS: versione più sicura di HTTP
  • Aprire un sito = inviare una richiesta HTTP
    • Richiesta GET
  • urlretrieve() esegue una GET
  • HTML - HyperText Markup Language
Importazione di dati in Python - livello intermedio

Richieste GET con urllib

from urllib.request import urlopen, Request
url = "https://www.wikipedia.org/"
request = Request(url)
response = urlopen(request)
html = response.read()
response.close()
Importazione di dati in Python - livello intermedio

Richieste GET con requests

ch_1_2.026.png

  • Usato da “Her Majesty's Government, Amazon, Google, Twilio, NPR, Obama for America, Twitter, Sony e istituzioni federali USA che preferiscono restare anonime”
Importazione di dati in Python - livello intermedio

Richieste GET con requests

  • Uno dei pacchetti Python più scaricati
import requests
url = "https://www.wikipedia.org/"
r = requests.get(url)
text = r.text
Importazione di dati in Python - livello intermedio

Passiamo alla pratica !

Importazione di dati in Python - livello intermedio

Preparing Video For Download...