rowrowrow
rowrowrow

Reputation: 1

get specific key and value from Web Scraping by using XML

How can code to find out fips when month is 2019, month is August and locality is King and Queen?

I started with import requests

from lxml import objectify


URL = 'https://data.virginia.gov/api/views/xvir-sctz/rows.xml?accessType=DOWNLOAD'

response = requests.get(URL).content

import requests
from lxml import objectify

#parsing the XML file
root = objectify.fromstring(response)

#take a look at the data
print(response)

Upvotes: 0

Views: 78

Answers (1)

Charles Han
Charles Han

Reputation: 2010

I will probably use panda dataframe for this as it is simpler to deal with the data and filtering:

import pandas as pd

df = pd.read_xml('https://data.virginia.gov/api/views/xvir-sctz/rows.xml?accessType=DOWNLOAD', xpath='row//row')[['year','month','fips','locality']]
df.columns = ['Year','Month','Fips','Locality']
print(df)

fips_df = df.query('Year == 2019.0 & Month == "August" & Locality.str.contains("King and Queen")')
print(fips_df)

Output like this:

enter image description here

You can change the query to suite your needs:

fips_df = df.query('Year == 2019.0 & Month == "August" & Locality.str.contains("King and Queen")')

Upvotes: 1

Related Questions