Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python获取Json数据中的特定项_Python_Json_Api - Fatal编程技术网

使用Python获取Json数据中的特定项

使用Python获取Json数据中的特定项,python,json,api,Python,Json,Api,我目前正在编写一个脚本,使用Hunter.io API查找基于域名的电子邮件 问题是,我的脚本返回一个JSON,其中包含很多详细信息,我只需要其中的邮件地址 这是我的密码: """script to find emails based on the domain name using Hunter.io""" import requests # To make get and post requests to APIs import

我目前正在编写一个脚本,使用Hunter.io API查找基于域名的电子邮件

问题是,我的脚本返回一个JSON,其中包含很多详细信息,我只需要其中的邮件地址

这是我的密码:

 """script to find emails based on the domain name using Hunter.io"""

import requests # To make get and post requests to APIs
import json # To deal with json responses to APIs
from pprint import pprint # To pretty print json info in a more readable format
from pyhunter import PyHunter # Using the hunter module

# Global variables

hunter_api_key = "API KEY" # API KEY 
contacts = [] # list where we'll store our contacts
contact_info = {} # dictionary where we'll store each contact information
limit = 1 # Limit of emails adresses pulled by the request
value = "value" # <- seems to be the key in Hunter's API of the email adress founded

# TODO: Section A - Ask Hunter to find emails based on the domain name

def get_email_from_hunter(domain_name,limit):
      
    url = "https://api.hunter.io/v2/domain-search"

    params = {
        "domain" : domain_name,
        "limit" : 1,
        "api_key" : hunter_api_key,
    }

    response = requests.get(url, params= params,)

    json_data = response.json()

    email_adress = json_data["data"]["emails"] # <- I have to find witch is the good key in order to return only the mail adress
    #pprint(email_adress)

    contact_info["email_adress"] = email_adress
    contact_info["domain_name"] = domain_name
    pprint(contact_info)
    return contact_info

get_email_from_hunter("intercom.io","1")
使用Hunter.io根据域名查找电子邮件的脚本 导入请求#向API发出get和post请求 导入json#以处理对API的json响应 从pprint导入pprint#到以更可读的格式打印json信息 从pyhunter使用hunter模块导入pyhunter# #全局变量 hunter_api_key=“api key”#api key contacts=[]#列出存储联系人的位置 contact_info={}#字典,我们将在其中存储每个联系人信息 limit=1#请求拉取的电子邮件地址的限制 value=“value”#
注意,在发布图像时未进行测试,而在发布文本时未测试json数据。

请看一下glom(),它使只获取所需字段变得简单。您的数据/电子邮件字段返回一个列表,因此您需要解析列表中的任何元素。非常感谢我检查此字段,我会回来与您分享您的一天
email_addresses = [item['value'] for item in json_data["data"]["emails"]]