Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Alirezam
Helper V
Helper V

Obtener el archivo comprimido csv de una URL

Hola

Me encontré con un sitio web que proporciona el archivo csv comprimido que desea obtener el csv en Power Bi. ¿Alguna indicación? Necesito obtener sólo el archivo en la primera fila superior todos los días.

dataset.PNG

5 REPLIES 5
lbendlin
Super User
Super User

Inspeccionar el HTML muestra que estos archivos ZIP están codificados por día

lbendlin_0-1599481242624.png

Por lo tanto, podría suponer que "el archivo de ayer" está disponible cuando lo sondea o ejecutar un rascador de pantalla.

Las suposiciones son aburridas, así que vamos con el segundo enfoque. Power Query es razonablemente hábil para leer tablas en páginas HTML y, en realidad, funciona bien con su URL.

let
    Source = Web.Page(Web.Contents("https://www.emi.ea.govt.nz/Wholesale/Datasets/RealTimePrices/2020")),
    Data0 = Source{0}[Data],
    ZipFile = Web.Contents("https://www.emi.ea.govt.nz/Wholesale/Datasets/RealTimePrices/2020/" & Data0[Name]{0}),
    ZipData = Unzip(ZipFile),
    #"Imported CSV" = Csv.Document(ZipData,[Delimiter=",", Columns=9, Encoding=1252, QuoteStyle=QuoteStyle.None])
in
    #"Imported CSV"

La función Descomprimir puede ser cualquiera que sea su preferencia, si desea tomar la de mi blog que necesita para enmendarlo ligeramente.

// expects binary of the ZIP file, only extracts the first data file after getting its size from the central directory
// https://en.wikipedia.org/wiki/Zip_(file_format)#Structure
(ZIPFile) =>
let
    //read the entire ZIP file into memory - we'll use it often so this is worth it
    Source = Binary.Buffer(ZIPFile),
    // get the full size of the ZIP file
    Size = Binary.Length(Source),
    //Find the start of the central directory at the sixth to last byte
    Directory = BinaryFormat.Record([ 
                    MiscHeader=BinaryFormat.Binary(Size-6), 
                    Start=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian)
            ]) ,
    Start = Directory(Source)[Start],
    //find the first entry in the directory and get the compressed file size
    FirstDirectoryEntry = BinaryFormat.Record([ 
                    MiscHeader=BinaryFormat.Binary(Start+20), 
                    FileSize=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
                    UnCompressedFileSize=BinaryFormat.Binary(4),
                    FileNameLen=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
                    ExtrasLen=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian)
            ]) ,
    //figure out where the raw data starts            
    Offset = 30+FirstDirectoryEntry(Source)[FileNameLen]+FirstDirectoryEntry(Source)[ExtrasLen],
    Compressed = FirstDirectoryEntry(Source)[FileSize]+1,
    //get the raw data of the compressed file
    Raw = BinaryFormat.Record([
                    Header=BinaryFormat.Binary(Offset), 
                    Data=BinaryFormat.Binary(Compressed)
            ]) 
    // unzip it
in 
    Binary.Decompress(Raw(Source)[Data], Compression.Deflate)

Y aquí está el resultado: (tenga en cuenta que los encabezados de columna no son parte de los datos, debe proporcionarlo usted mismo).

lbendlin_1-1599482764721.png

Gracias por su gran ayuda. Estaba fuera tan siento por la respuesta tardía. Apliqué el código que envió (la primera parte corta) en la consulta en blanco y obtuve pocos errores como adjuntos. No pude 1.PNG 2.PNG 3.PNG pasar. y lo que veo es la lista de nombres de archivo comprimidos no su contenido:

necesitará la función Descomprimir también - ese es el código en el segundo listado.

Greg_Deckler
Super User
Super User

@Alirezam Echa un vistazo a este artículo de blog de @lbendlin, parece ser exactamente lo que estás buscando:

https://community.powerbi.com/t5/Community-Blog/Working-With-Zip-Files-in-Power-Query/ba-p/1190186


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
amitchandak
Super User
Super User

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.