Skip to content
Advertisement

Troubles merging two json urls

First of all, I am getting this error. When I try running pip3 install --upgrade json in an attempt to resolve the error, python is unable to find the module. The segment of code I am working with can be found below the error, but some further direction as for the code itself would be appreciated.

Error:

Traceback (most recent call last):
  File "Chicago_cp.py", line 18, in <module>
    StopWork_data      = json.load(BeautifulSoup(StopWork_response.data,'lxml'))
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
TypeError: 'NoneType' object is not callable

Script:

#!/usr/bin/python

import json
from bs4 import BeautifulSoup
import urllib3
http = urllib3.PoolManager()

# Define Merge
def Merge(dict1, dict2):
    res = {**dict1, **dict2}
    return res

# Open the URL and the screen name
StopWork__url = "someJsonUrl"
Violation_url = "anotherJsonUrl"

StopWork_response  = http.request('GET', StopWork__url)
StopWork_data      = json.load(BeautifulSoup(StopWork_response.data,'lxml'))

Violation_response = http.request('GET', Violation_url)
Violation_data     = json.load(BeautifulSoup(Violation_response.data,'lxml'))

dict3 = Merge(StopWork_data,Violation_data)
print (dict1)

Advertisement

Answer

json.load expects a file object or something else with a read method. The BeautifulSoup object doesn’t have a method read. You can ask it for any attribute and it will try to find a child tag with that name, i.e. a <read> tag in this case. When it doesn’t find one it returns None which causes the error. Here’s a demo:

import json
from bs4 import BeautifulSoup

soup = BeautifulSoup("<p>hi</p>", "html5lib")
assert soup.read is None
assert soup.blablabla is None

assert json.loads is not None
json.load(soup)

Output:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    json.load(soup)
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
TypeError: 'NoneType' object is not callable

If the URL is returning JSON then you don’t need BeautifulSoup at all because that’s for parsing HTML and XML. Just use json.loads(response.data).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement