繁体   English   中英

Web Scraping:在Python中解析JSON时获取KeyError

[英]Web Scraping: getting KeyError when parsing JSON in Python

我想从网页中提取完整地址,我正在使用BeautifulSoup和JSON。 这是我的代码:

import bs4
import json
from bs4 import BeautifulSoup
import requests

url = 'xxxxxxxxxxxxxxxxx'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')

for i in soup.find_all('div', attrs={'data-integration-name':'redux-container'}):
    info = json.loads(i.get('data-payload'))

我打印出“信息”:

{'storeName': None, 'props': {'locations': [{'dirty': False, 'updated_at': '2016-05-05T07:57:19.282Z', 'country_code': 'US', 'company_id': 106906, 'longitude': -74.0001954, 'address': '5 Crosby St  3rd Floor', 'state': 'New York', 'full_address': '5 Crosby St  3rd Floor, New York, 10013, New York, USA', 'country': 'United States', 'id': 17305, 'to_params': 'new-york-us', 'latitude': 40.719753, 'region': '', 'city': 'New York', 'description': '', 'created_at': '2015-01-19T01:32:16.317Z', 'zip_code': '10013', 'hq': True}]}, 'name': 'LocationsMapList'}

我想要的是“位置”下的“full_address”,所以我的代码是:

info = json.loads(i.get('data-payload'))
for i in info['props']['locations']:
        print (i['full_address'])

但我得到了这个错误:

----> 5     for i in info['props']['locations']:

KeyError: 'locations'

我想打印完整的地址,即'5 Crosby St 3rd Floor,New York,10013,New York,USA'。

非常感谢!

您正在解析的数据似乎不一致,键不在所有对象中。

如果您仍想执行循环,则需要使用try / except语句来捕获异常,或者当您在字典中查找可能不在此处的密钥时,方法get设置回退。

info = json.loads(i.get('data-payload'))
for item in info['props'].get('locations', []):
    print (item.get('full_address', 'no address'))

get('locations', []) :如果键location不存在,则返回一个空列表,因此循环不会运行任何迭代。

get('full_address', 'no address') :如果没有这样的密钥,则返回“no get('full_address', 'no address')


编辑:

数据不一致从不信任数据 )。 某些JSON对象具有带null / None值的键props 下一个修复应该纠正:

info = json.loads(i.get('data-payload'))
if info.get('props'):
    for item in info['props'].get('locations', []):
        print (item.get('full_address', 'no address'))

你的第一个对象很好,但很明显你的第二个对象在任何地方都没有locations键,也没有full_address

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM