I need to download and import data from a web like this Berufsauspendler to QGIS, but with Python Console in QGIS. After that I need to store this Data in my computer.

I don't have much experience with Python.

I have to import another data from this web, but in link, I just need to change 19321-106i instead 19321-108i from that link. Is there a quick way to import this data QGIS?


Have a look at https://www.tutorialspoint.com/downloading-files-from-web-using-python and Add layer to map in qgis3 via a python script

As the first part of your question was really about Python -it was important to search without QGIS in your search. The 2nd part was about QGIS but was just about loading data.

The code below is from the two answers above. I am just pointing this out to you as a new user on this platform so that you can get more value from it.

In your case in the python window, it's just

import requests
from qgis.core import QgsVectorLayer, QgsProject 

# Set variables
url = 'https://www.landesdatenbank.nrw.de/ldbnrwws/rest/2020/data/table?username=LD001583%20&password=N0key1993!&name=19321-108i&area=all&compress=false&transpose=false&startyear=&endyear=&timeslices=&regionalvariable=&regionalkey=&classifyingvariable1=GEMEIN&classifyingkey1=05766020&classifyingvariable2=&classifyingkey2=&classifyingvariable3=&classifyingkey3=&job=false&Stand=30.06.2019&language=de'
downloaded_file = 'c:/temp/test.txt'
file_name = 'test'

# No more changes required
r = requests.get(url, allow_redirects=True)

#Import data based on url above.
open(downloaded_file, 'wb').write(r.content)

#Add imported data into QGIS

layer = QgsVectorLayer(downloaded_file, file_name, "ogr")

I am not sure what file type the downloaded file is, so just change test.txt and the path to whatever you need. You may also need to remove the first line or some other parts of the downloaded file to make it into an OGR readable dataset.

  • thank you for helping, i tried to add this to Python Console in QGIS and it said "exec(open('C:/Users/nguye/AppData/Local/Temp/tmpvgb919pl.py'.encode('utf-8')).read()) False" Beside it has a Error in this Layer, it said:"Layer's Data is not found". I am really confused with this Error, maybe this Data is not really good to add in QGIS
    – AnAn
    Dec 5 '21 at 2:20
  • You can't open .py files as a data layer. The data in the link you sent seems to be in geojson or some other format that I am not to familiar with. You need to work out what the extension should be and then QGIS should be able to open it. Maybe post a separate question asking what the data type for this is and just include the download link as well as the first few and last few lines of it in the question.
    – GeorgeC
    Dec 5 '21 at 3:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.