Get Value from JSON or XML (WHOIS API) Python - python

I need to take only one value from an XML/JSON file I read into a value.
https://whoisapi.whoisxmlapi.com/whois/google.com?format=XML
here is my code example :
import json
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
domainName = 'google.com';
apiKey = 'YourApiKey'
url = 'https://www.whoisxmlapi.com/whoisserver/WhoisService?'\
+ 'domainName=' + domainName + '&apiKey=' + apiKey + "&outputFormat=JSON"
testing=urlopen(url).read().decode('utf8')
test = json.loads(whoisdb)
registrar = test['WhoisRecord']['registrarName']
domain['whois-created'] = test['WhoisRecord']['createdDate']
And if I am using the value "registrar" later, I get a "Keyerror".
Unfortunaly neither of the other Questions helped me...
Edit : https://i.stack.imgur.com/FMBjW.png ,well he gets the value in the variable, but I still get this error.(I printed the error while running)

Related

How to remove ('') from string [duplicate]

This question already has answers here:
How can I parse a dictionary string?
(2 answers)
Closed 2 years ago.
I am writing code to obtain geolocation information using the IP Geolocation API. I used the following code in my jupyter notebook...
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
ip = '8.8.8.8'
api_key = 'your_api_key'
api_url = 'https://geo.ipify.org/api/v1?'
url = api_url + 'apiKey=' + api_key + '&ipAddress=' + ip
result = urlopen(url).read().decode('utf8')
print(result)
I got the following result, but it returns the following string...
'{"ip":"8.8.8.8","location":{"country":"US","region":"California","city":"Mountain View","lat":37.4223,"lng":-122.085,"postalCode":"94043","timezone":"-07:00","geonameId":5375480},"domains":["0--9.ru","000.lyxhwy.xyz","000180.top","00049ok.com","001998.com.he2.aqb.so"],"as":{"asn":15169,"name":"Google LLC","route":"8.8.8.0\\/24","domain":"https:\\/\\/about.google\\/intl\\/en\\/","type":"Content"},"isp":"Google LLC"}'
I am trying to remove the strings at the beginning and end. I tried changing this string to a list by calling the list function on the result variable but that didn't work. I would like to obtain the following output...
{"ip":"8.8.8.8","location":{"country":"US","region":"California","city":"Mountain View","lat":37.4223,"lng":-122.085,"postalCode":"94043","timezone":"-07:00","geonameId":5375480},"domains":["0--9.ru","000.lyxhwy.xyz","000180.top","00049ok.com","001998.com.he2.aqb.so"],"as":{"asn":15169,"name":"Google LLC","route":"8.8.8.0\\/24","domain":"https:\\/\\/about.google\\/intl\\/en\\/","type":"Content"},"isp":"Google LLC"}
By doing this, I will get a dictionary which I can work with using the various keys. Any help will be greatly appreciated.
You can use json library.
import json
str = '{"ip": "8.8.8.8"}'
res = json.loads(str)
print(res)
Result will be a dict.
If you want a dictionary, you shouldn't try to remove the quotes, that is there because it denotes a string variable. You should instead use JSON as this is a valid JSON string:
import json
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
ip = '8.8.8.8'
api_key = 'your_api_key'
api_url = 'https://geo.ipify.org/api/v1?'
url = api_url + 'apiKey=' + api_key + '&ipAddress=' + ip
result = json.loads(urlopen(url).read().decode('utf8'))
print(result)
This will give you the dictionary you are looking for.

Unable to extract the table from API using python

I am trying to extract a table using an API but I am unable to do so. I am pretty sure that I am not using it correctly, and any help would be appreciated.
Actually I am trying to extract a table from this API but unable to figure out the right way on how to do it. This is what is mentioned in the website. I want to extract Latest_full_data table.
This is my code to get the table but I am getting error:
import urllib
import requests
import urllib.request
locu_api = 'api_Key'
def locu_search(query):
api_key = locu_api
url = 'https://www.quandl.com/api/v3/databases/WIKI/metadata?api_key=' + api_key
response = urllib.request.urlopen(url).read()
json_obj = str(response, 'utf-8')
datanew = json.loads(json_obj)
return datanew
When I do print(datanew). Update: Even if I change it to return data new, error is still the same.
I am getting this below error:
name 'datanew' is not defined
I had the same issues with urrlib before. If possible, try to use requests it's a better designed and working library in my opinion. Also, it is capable of reading JSON with a single function so no need to run it through multiple lines Sample code here:
import requests
locu_api = 'api_Key'
def locu_search():
url = 'https://www.quandl.com/api/v3/databases/WIKI/metadata?api_key=' + api_key
return requests.get(url).json()
locu_search()
Edit:
The endpoint that you are calling might not be the correct one. I think you are looking for the following one:
import requests
api_key = 'your_api_key_here'
def locu_search(dataset_code):
url = f'https://www.quandl.com/api/v3/datasets/WIKI/{dataset_code}/metadata.json?api_key={api_key}'
req = requests.get(url)
return req.json()
data = locu_search("FB")
This will return with all the metadata regarding a company. In this case Facebook.
Maybe it doesn't apply to your specific problem, but what I normally do is the following:
import requests
def get_values(url):
response = requests.get(url).text
values = json.loads(response)
return values

Python 2 to Python 3 : TypeError: 'module' object is not callable [duplicate]

This question already has answers here:
How to use urllib with username/password authentication in python 3?
(3 answers)
Closed 4 years ago.
I'm trying to modify a code which was written in Python 2 Language with the urllib2 module.I did modified my code with the module urllib in Python 3 but I'm getting error :
req = urllib.request(url)
TypeError: 'module' object is not callable
What I am doing wrong here?
import urllib.request
import json
import datetime
import csv
import time
app_id = "172"
app_secret = "ce3"
def testFacebookPageData(page_id, access_token):
# construct the URL string
base = "https://graph.facebook.com/v2.4"
node = "/" + page_id
parameters = "/?access_token=%s" % access_token
url = base + node + parameters
# retrieve data
req = urllib.request(url)
response = urllib.urlopen(req)
data = json.loads(response.read())
print (json.dumps(data, indent=4, sort_keys=True))
Change the lines
req = urllib.request(url)
response = urllib.urlopen(req)
to:
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
You can find more information on this module **https://docs.python.org/3/library/urllib.request.html#urllib.request.Request
**https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen
#kvmahesh's answer is absolutely correct. I'll just provide an alternate solution which supports both the versions. Use Python's requests library for making the call.
import requests
import json
import datetime
import csv
import time
app_id = "172"
app_secret = "ce3"
def testFacebookPageData(page_id, access_token):
# construct the URL string
base = "https://graph.facebook.com/v2.4"
node = "/" + page_id
parameters = "/?access_token=%s" % access_token
url = base + node + parameters
# retrieve data
response = requests.get(url)
data = json.loads(response.text())
print (json.dumps(data, indent=4, sort_keys=True))
For detailed usage of requests: Python Requests Docs
urllib.request is a module. You are calling the module on line 22...
req = urllib.request(url)
To fix do following:
1) Import at the top:
from urllib.request import urlopen
2) then pass the url to urlopen(url)
# remove this line req = urllib.request(url)
response = urlopen(url)
data = json.loads(response.read())
3) See similar error here
TypeError: 'module' object is not callable

Python-JSON - How to parse API output?

I'm pretty new.
I wrote this python script to make an API call from blockr.io to check the balance of multiple bitcoin addresses.
The contents of btcaddy.txt are bitcoin addresses seperated by commas. For this example, let it parse this.
import urllib2
import json
btcaddy = open("btcaddy.txt","r")
urlRequest = urllib2.Request("http://btc.blockr.io/api/v1/address/info/" + btcaddy.read())
data = urllib2.urlopen(urlRequest).read()
json_data = json.loads(data)
balance = float(json_data['data''address'])
print balance
raw_input()
However, it gives me an error. What am I doing wrong? For now, how do I get it to print the balance of the addresses?
You've done multiple things wrong in your code. Here's my fix. I recommend a for loop.
import json
import urllib
addresses = open("btcaddy.txt", "r").read()
base_url = "http://btc.blockr.io/api/v1/address/info/"
request = urllib.urlopen(base_url+addresses)
result = json.loads(request.read())['data']
for balance in result:
print balance['address'], ":" , balance['balance'], "BTC"
You don't need an input at the end, too.
Your question is clear, but your tries not.
You said, you have a file, with at least, more than registry. So you need to retrieve the lines of this file.
with open("btcaddy.txt","r") as a:
addresses = a.readlines()
Now you could iterate over registries and make a request to this uri. The urllib module is enough for this task.
import json
import urllib
base_url = "http://btc.blockr.io/api/v1/address/info/%s"
for address in addresses:
request = urllib.request.urlopen(base_url % address)
result = json.loads(request.read().decode('utf8'))
print(result)
HTTP sends bytes as response, so you should to us decode('utf8') as approach to handle with data.

Wunderground api search bar

I'm currently writting a program that will search for the weather.
I'm trying to create an option where you can search your location however it doesn't seem to be working.
from urllib.request import Request, urlopen
import json
import re
location = input('location you would like to know the weather for')
API_KEY = '<API-KEY>'
url = 'http://python-weather-api.googlecode.com/svn/trunk/ python-weather-api' + API_KEY +'/geolookup/conditions/q/IA/'+ location +'.json'
response = urllib.request.Request(url)
def response as request(url)
json_string = response.read().decode('utf8')
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
print("Current temperature in %s is: %s" % (location, temp_f))
response.close()
I keep on recieving this error
Can't comment yet (reputation too low since I just joined SO), but regarding your "urllib is not defined" issue, that has to do with how you import the urlopen function.
Instead of:
urllib.urlopen(url)
try:
urlopen(url)
EDIT: Here's your code, fixed:
from urllib.request import urlopen
import json
location = input('location you would like to know the weather for')
API_KEY = '<API-KEY>'
url = 'http://api.wunderground.com/api/' + API_KEY + '/geolookup/conditions/q/IA/'+ str(location) +'.json'
response = urlopen(url)
json_string = response.read().decode('utf8')
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
print("Current temperature in %s is: %s" % (location, temp_f))
Works fine for Tama and other cities in IA. Watch out though, place names like Des Moines won't work, because spaces aren't allowed in the URLs - you'll have to take care of that. (The example for the API suggests _ for spaces http://www.wunderground.com/weather/api/d/docs?MR=1). Good luck!
Hey not sure if you are still stuck on this but here is my wunderground project that should have what you're looking for https://github.com/Oso9817/weather_texts
Where you have str.input, you need to use str(location). Right now if you go into a python repl and type str, you will find out that it is a reserved keyword. You want to use the variable location you are getting from the input of the user and not the str object.

Categories

Resources