La conversione eurostat sommario PDF a JSON

0

Domanda

Dal momento che non c'è accessibile API per i dati di EUROSTAT, ogni query deve essere creata manualmente ho trovato questa tabella di contenuti E voglio estrarre un archivio in formato JSON. Nel file ci sono i titoli delle sezioni, e ogni foglia ha 3 link, Ma come faccio a collegare i link e i titoli e sezioni in un JSON?

Ho questo codice di base:

PDFFile = open("table_of_contents_en.pdf",'rb')

PDF = PyPDF2.PdfFileReader(PDFFile)
pages = PDF.getNumPages()
key = '/Annots'
uri = '/URI'
ank = '/A'

for page in range(1,2):
    print("Current Page: {}".format(page))
    pageSliced = PDF.getPage(page)
    pageObject = pageSliced.getObject()
    if key in pageObject.keys():
        ann = pageObject[key]
        for a in ann:
            u = a.getObject()
            if uri in u[ank].keys():
                print(u[ank][uri])

E questo per il testo:

pdfFileObj = open('table_of_contents_en.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
print(pdfReader.numPages)
pageObj = pdfReader.getPage(0)
print(pageObj.extractText())
pdfFileObj.close()

e questo per il download zip:

for page in range(1,2):
    print("Current Page: {}".format(page))
    pageSliced = PDF.getPage(page)
    pageObject = pageSliced.getObject()
    if key in pageObject.keys():
        ann = pageObject[key]
        for a in ann:
            u = a.getObject()
            if uri in u[ank].keys():
                print(u[ank][uri])
            if str(u[ank].keys()).find(".tsv.gz") != -1 :
                url = str(u[ank].keys())
                r = requests.get(url, allow_redirects=True)
                print(str(str(str(u[ank].keys()).split("/")[-1]).split(".")[0])
                open(str(str(str(u[ank].keys()).split("/")[-1]).split(".")[0]), 'wb').write(r.content)

Ma come posso fare questo, allo stesso tempo, correttamente, in alcuni dati strutturati?

database json pdf python
2021-11-21 05:20:43
1

Migliore risposta

0

Provare come questo, questo non sarà un JSON, ma la lista dei file contenuti in una cartella possono essere convertiti, ecco come ottenere i file:

import PyPDF2
import requests

PDFFile = open("table_of_contents_en.pdf",'rb')

PDF = PyPDF2.PdfFileReader(PDFFile)
pages = PDF.getNumPages()
key = '/Annots'
uri = '/URI'
ank = '/A'

for page in range(pages):
    print("Current Page: {}".format(page))
    pageSliced = PDF.getPage(page)
    pageObject = pageSliced.getObject()
    if key in pageObject.keys():
        ann = pageObject[key]
        for a in ann:
            u = a.getObject()
            # if uri in u[ank].keys():
            #   print(u[ank][uri])
            if str(u[ank][uri]).find(".tsv.gz") != -1 :
                url = str(u[ank][uri])
                try:
                    r = requests.get(url, allow_redirects=True)
                    print(str(str(str(url).split("/")[-1]).split(".")[0]))
                    open(str(str(url).split("/")[-1]), 'wb').write(r.content)
                except:
                    print("ERROR ON" + url)

Utilizzo:

  1. Scarica la tabella dei contenuti del file da Eurostat: http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&file=table_of_contents_en.pdf
  2. eseguire python3 il codice di cui sopra, modificato con il nome del file, se è cambiato.
  3. estrarre con alcuni tool scaricato .gz file (suggerimento: Come decomprimere gz file utilizzando Python )
  4. eliminare il .gz i file.

Codice completo su: https://github.com

2021-11-21 16:14:06

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................