【问题标题】:Issues when processing json data处理 json 数据时的问题
【发布时间】:2017-11-05 19:05:56
【问题描述】:

目前,当输入是公司时,例如 LinkedIn,代码运行良好并返回正确的消息。电子邮件输入根本不起作用。有什么建议么?当前代码如下:

import urllib
import json

userInput = input('Do you want to find an email (E) or company (C)? ')
userInput = userInput.upper()

def getResponse(url):
    cleanUrl = urllib.parse.quote_plus(url, safe=':/?&=')
    try:
        myRequest = urllib.request.Request(cleanUrl, headers={'User-Agent': 'Chrome'})
        myResponse = urllib.request.urlopen(myRequest).read().decode('utf-8')
        myJSON = json.loads(myResponse)
        return myJSON
    except urllib.error.HTTPError as err:
        print('We failed with error code - ', err.code, '(', err.reason, ')')
        return
    except:
        print("Something else went terribly wrong!")

def takeInput():
    url = 'https://haveibeenpwned.com/api/v2/'
    if userInput == 'E':
        email = input('What email do you want me to search for? ')
        fixedurl = url + 'breachedaccount/' + email
    elif userInput == 'C':
        company = input('What company do you want me to search for? ')
        fixedurl = url + 'breach/' + company 
    else:
        print('Invalid entry. Please type E or C.')

    myJSON = getResponse(fixedurl)
    for data in myJSON:
        name = myJSON.get("Name")
        date = myJSON.get("BreachDate")
        count = myJSON.get("PwnCount")
        itemsLost = myJSON.get("DataClasses")
        print("The", name,"breach occurred on", date,"and", count,"accounts were compromised. Iteams lost include:")
        print(itemsLost)
        break

takeInput()

【问题讨论】:

    标签: python json python-3.x urllib


    【解决方案1】:

    我没有看到休息; 例如:

    while True:
         try:
             x = int(input("Please enter a number: "))
             break
         except ValueError:
             print("Oops!  That was no valid number.  Try again...")
    

    如果你想用另一种方式做,你可以顺便检查一下这个文档。取决于你的目标:

    http://ozgur.github.io/python-linkedin/

    【讨论】:

    • 谢谢!添加休息帮助。我仍然无法运行电子邮件方面的工作。我不断收到“TypeError:'NoneType' 对象不可迭代”
    • 你应该顺便使用 "" fixedurl = url + 'breachedaccount/' + email ""
    猜你喜欢
    • 2019-01-20
    • 2019-04-01
    • 2020-10-16
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多