Getting Error "KeyError" when extracting JSON values - python

I am successful in extracting the response from a JSON. However, I am unable to list all or extract what I need on the key and its pair
Below is my code:
import requests
response = requests.get("https://www.woolworths.com.au/apis/ui/Product/Specials/half-price?GroupID=948&isMobile=false&pageNumber=1&pageSize=36&richRelevanceId=SP_948&sortType=Personalised")
data = response.json()
I tried to do data['Stockcode']
but no luck or I use data['Product']
It says:
>>> data['Product']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Product'
>>> data['Products']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Products'

try:
>>> data['Items'][0]['Products']
Print data and see its data structure How its constructed then you can extract values as per your need

Related

Retrieve python traceback format from string

I have a string that is a python traceback collapsed into a single line. I want to retrieve the multiline format of the traceback for the sake of readability. What is the best way to achieve it?
I see that traceback module has similar functionality, but I don't see how to pass a string as an argument there:
https://docs.python.org/3/library/traceback.html
Example input:
'Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception'
Example output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception

The module browserhistory doesn't seem to work and raises KeyError

Running the example from pip modified to choose the 'chrome' browser, I get a KeyError
script:
import browserhistory as bh
dict_obj = bh.get_browserhistory()
data = dict_obj.keys()
print(data)
data = dict_obj['chrome'][0]
print(data)
output:
dict_keys([])
Traceback (most recent call last):
File "/home/raj/Desktop/anilyzer/myproject/main.py", line 6, in
data = dict_obj['chrome'][0]
KeyError: 'chrome'
What is happening?
It doesn't look like the module is maintained and cannot detect your browser(s)
https://github.com/kcp18/browserhistory
The function bh.get_browserhistory() should return a dictionary where the type of browser is the lookup key. Displaying this as you do shows that the dictionary is empty.
>>> data = dict_obj.keys()
>>> print(data)
dict_keys([])
This is why you get a KeyError when attempting to read a specific key from the dict
>>> d = {}
>>> type(d)
<class 'dict'>
>>> d["something"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'something'
However, there appears to be a different project which may do what you want https://github.com/pesos/browser-history
This is from reading the GitHub Issues: https://github.com/kcp18/browserhistory/issues and I cannot comment on its quality or safety!

python email decode_header raise HeaderParseError

I got a HeaderParseError as below.What's wrong with it?
>>> from email import Header
>>> s= "=?UTF-8?B?6KGM6KGM5ZyI5Li65oKo5o6o6I2Q5Lul5LiL6IGM5L2N77yM?==?UTF-8?B?56Wd5oKo5om+5Yiw5aW95bel5L2c77yB44CQ6KGM6KGM5ZyI44CR?="
>>> src = Header.decode_header(s)
This is the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/email/header.py", line 108, in decode_header
raise HeaderParseError
email.errors.HeaderParseError
You are trying to parse two headers at once:
"=?UTF-8?B?6KGM6KGM5ZyI5Li65oKo5o6o6I2Q5Lul5LiL6IGM5L2N77yM?="
and
"=?UTF-8?B?56Wd5oKo5om+5Yiw5aW95bel5L2c77yB44CQ6KGM6KGM5ZyI44CR?="
removing one of them will do the job. If you want to parse all of them - you have to split them first

why do I get the following error in get request

I have code like this :
import requests
import xmltodict
Locations = ['http://129.94.5.93:49154/setup.xml', 'http://129.94.5.92:49154/setup.xml', 'http://129.94.5.93:49154/setup.xml', 'http://129.94.5.92:49154/setup.xml', 'http://129.94.5.95:80/description.xml']
for item in Locations:
r = requests.get(item)
reply = xmltodict.parse(r.text)
this gives me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ssdpxml.py", line 57, in <module>
r = requests.get(item)
AttributeError: 'str' object has no attribute 'get'
But it works when I do this:
r=requests.get('http://129.94.5.95:80/description.xml')
Why do I get the above mentioned error??

How to scrape a XML website using bs4?

I am parsing websites which sell electronic products..
Specifically, I am looking to collect the name and the price of the product
I ran into a small problem when parsing a xml based site....
Here is my code:
>>> import urllib2
>>> from bs4 import BeautifulSoup
>>> url=urllib2.urlopen("http://store.explorelabs.com/index.php?main_page=products_all")
>>> soup=BeautifulSoup(url,"xml")
>>> data=soup.find_all(colspan="2")
The code above works
now when I do this (as the name is inside the strong tags)
>>> data.strong
or
>>> data.attrs
It shows me this:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
data.strong
AttributeError: 'ResultSet' object has no attribute 'strong'
or
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
data.find_all('a')
AttributeError: 'ResultSet' object has no attribute 'find_all'
I am trying to iterate and try to find out more.
Any pointers would be very helpful.
find_all returns a list of elements that match, not one. Loop over the result set to get the individual items:
for element in data:
element.attrs

Categories

Resources