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.
Related
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
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)
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
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.
I have been struggling for a while trying to convert some code from an older version of Python. I'm simply trying to run an api lookup from wunderground and I can't get past my errors in python. Here is the error:
f = urllib.request.urlopen(fileName)
AttributeError: 'module' object has no attribute 'request'
The code is pretty straight forward, I know i"m missing something simple, thanks for any help.
import urllib
import json
key = "xxxxxxxxxxxxxxxxx"
zip = input('For which ZIP code would you like to see the weather? ')
fileName = "http://api.wunderground.com/api/" + key + "/geolookup/conditions/q/PA/" + zip + ".json"
f = urllib.request.urlopen(fileName)
json_string = f.read()
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)")
close()
Sometimes importing a package (e.g. numpy) automatically imports submodules (e.g. numpy.linalg) into its namespace. But that is not the case for urllib. So you need to use
import urllib.request
instead of
import urllib
in order to access the urllib.request module. Alternatively, you could use
import urllib.request as request
in order to access the module as request.
Looking at the examples in the docs is a good way to avoid problems like this in the future.
Since f.read() returns a bytes object, and json.loads expects a str, you'll also need to decode the bytes. The particular encoding depends on what the server decides to send you; in this case the bytes are utf-8 encoded. So use
json_string = f.read().decode('utf-8')
parsed_json = json.loads(json_string)
to decode the bytes.
There is a small typo on the last line. Use
print ("Current temperature in %s is: %s" % (location, temp_f))
to interpolate the string "Current temperature in %s is: %s" with the values (location, temp_f). Note the placement of the quotation mark.
Tip: Since zip is a builtin-function, it is a good practice not to name a
variable zip since this changes the usual meaning of zip making it harder
for others and perhaps future-you to understand your code. The fix is easy: change zip to something else like zip_code.
import urllib.request as request
import json
key = ...
zip_code = input('For which ZIP code would you like to see the weather? ')
fileName = "http://api.wunderground.com/api/" + key + "/geolookup/conditions/q/PA/" + zip_code + ".json"
f = request.urlopen(fileName)
json_string = f.read().decode('utf-8')
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))
I would recommend using the requests library Python HTTP for Humans., the code below will work on either python2 or 3:
import requests
key = "xxxxxxxxxxx"
# don't shadow builtin zip function
zip_code = input('For which ZIP code would you like to see the weather? ')
fileName = "http://api.wunderground.com/api/{}/geolookup/conditions/q/PA/{}.json".format(key, zip_code)
parsed_json = requests.get(fileName).json()
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
# pass actual variables and use str.format
print ("Current temperature in {} is: {}%f".format(location, temp_f))
Getting the json is simply requests.get(fileName).json(), using str.format is the preferred method and I find less prone to errors, it is also far more feature-rich in comparison to the older printf style formatting.
You can see it works under both 2 and 3 with a sample run:
:~$ python3 weat.py
For which ZIP code would you like to see the weather? 12212
Current temperature in Albany is: 68.9%f
:~$ python2 weat.py
For which ZIP code would you like to see the weather? 12212
Current temperature in Albany is: 68.9%f