0

Currently I using ParseHub to scrape some basic data about a list counties, the json file for this can be seen below. I also want to scrape the current time of each country which means going to other website were such information can be found, but the list of counties on that website are in a complete different order meaning each country would end up with the incorrect time.

Is there a was I can scrape the time of each country and have it appended to the correct countries json object or am I thinking about this the wrong way?

country.json

{
 "country": [
  {
   "name": "China",
   "pop": "1,438,801,917",
   "area": "9,706,961 km²",
   "growth": "0.39%",
   "worldPer": "18.47%",
   "rank": "1"
  },
  {
   "name": "India",
   "pop": "1,378,687,736",
   "area": "3,287,590 km²",
   "growth": "0.99%",
   "worldPer": "17.70%",
   "rank": "2"
  },
  {
   "name": "United States",
   "pop": "330,812,025",
   "area": "9,372,610 km²",
   "growth": "0.59%",
   "worldPer": "4.25%",
   "rank": "3"
  }
{

time.json

{
 "country": [
  {
   "name": "china",
   "time": "18:36"
  }
{

How would I go about adding this data to the China object in country.json

1 Answers1

1

Try this:

import json

with open('country.json') as f1, open('time.json') as f2:
    country = json.loads(f1.read())
    time = json.loads(f2.read())

country = {x['name'].lower(): x for x in country['country']}

for y in time['country']:
    if y['name'].lower() in country:
        country[y['name'].lower()]['time'] = y['time']

country = {'country': list(country.values())}

with open('country.json', 'w') as fw:
    json.dump(country, fw)

Output: country.json

{
    "country": [
        {
            "name": "China",
            "pop": "1,438,801,917",
            "area": "9,706,961 km²",
            "growth": "0.39%",
            "worldPer": "18.47%",
            "rank": "1",
            "time": "18:36"
        },
        {
            "name": "India",
            "pop": "1,378,687,736",
            "area": "3,287,590 km²",
            "growth": "0.99%",
            "worldPer": "17.70%",
            "rank": "2"
        },
        {
            "name": "United States",
            "pop": "330,812,025",
            "area": "9,372,610 km²",
            "growth": "0.59%",
            "worldPer": "4.25%",
            "rank": "3"
        }
    ]
}
deadshot
  • 7,656
  • 4
  • 13
  • 31