Skip to content
Advertisement

Python Read Website Table Data into Dataframe

I came to know this source to import data. I tried but not successful in importing the data

https://public.opendatasoft.com/explore/embed/dataset/us-zip-code-latitude-and-longitude/table/

my code:

from urllib.request import urlopen, Request
### Import USA ZIP codes,counties, latitudes
usurl = 'https://public.opendatasoft.com//explore//embed//dataset//us-zip-code-latitude-and-longitude//table//'
query_url = Request(usurl)
url_response = urlopen(query_url)
read_response = url_response.read()

print (read_response)
b'n<!DOCTYPE html>n<html lang="en">n    <head>n    n    nn    n    
    <title>Opendatasoft</title>n        <link rel="stylesheet"
 type="text/css" href="/static/vendor/font-awesome-4.7.0/css/font-awesome.min.css">n        <link rel="styleshe....

Presently I see no data but a string text.

Table on the data: enter image description here

Advertisement

Answer

JS is creating the table and rendering of javascript in a request does not work. a workaround can be:

url='https://public.opendatasoft.com/explore/dataset/us-zip-code-latitude-and-longitude/download/?format=csv&timezone=America/New_York&lang=en&use_labels_for_header=true&csv_separator=%3B' 
df=pd.read_csv(url,sep=";")
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement