I just used gogole api to search via python and used this script below
import urllib
import json as m_json
query = raw_input ( 'Query: ' )
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
url = result['url'] # was URL in the original and that threw a name error exception
print ( url )
and after that I got result below :
Query: inurl:"readnews.php?id="
http://www.idmantv.az/readnews.php%3Fid%3D14999
http://www.kanda.com/readnews.php%3Fid%3D9
http://www.dcfever.com/news/readnews.php%3Fid%3D12573
http://www.thegrower.org/readnews.php%3Fid%3D6c0p5n0e8i6b
but I want this url in normal form like
http://www.idmantv.az/readnews.php?id=14999
How to do that with python?
Use urllib.unquote or urllib.unquote_plus to decode %-encoded string:
>>> urllib.unquote('http://www.idmantv.az/readnews.php%3Fid%3D14999')
'http://www.idmantv.az/readnews.php?id=14999'
Related
I am using Boto3 in python to get the data. I have created an API with the help of Flask in Python. I am firing a query and trying to retrieve the data, but it is too large hence trying to use Paginator but it returns NULL everytime.
paginator = DataClient.get_paginator('get_statement_result')
PaginatorResponse = paginator.paginate(
Id=request.args.get('Id'),
PaginationConfig={
'MaxItems': request.args.get('max-items'),
'StartingToken': request.args.get('start-token')
}
)
RESPONSE['redshift'] = PaginatorResponse
response = app.response_class(
response = json.dumps(RESPONSE ,sort_keys=True,indent=1,default=default),
status = STATUSCODE,
mimetype = 'application/json'
)
This is the paginator code which returns NULL on same query ID which I use in the get_statement_result directly. Here is the code for the same:
RecordsResponse = DataClient.get_statement_result(Id=request.args.get('Id'))
RESPONSE['redshift'] = RecordsResponse
RESPONSE['records'] = convertRecords(RecordsResponse)
response = app.response_class(
response = json.dumps(RESPONSE ,sort_keys=True,indent=1,default=default),
status = STATUSCODE,
mimetype = 'application/json'
)
Result ScreenShot from Get Statement Result
Result ScreenShot from Get Paginator
Not able to understand where I am going wrong, and how to get it done.
I have created a post route using werkzeug.
http://localhost:8000/v1/api/<serial_id>/data
def url_map():
tenants = [
Submount(
"/<serial_id>",
[
Rule(
"/data",
methods=["POST"],
endpoint=getData,
)
],
)
]
api = [Submount("/api", api)]
rules = [Submount("/v1", api)]
return Map(rules, strict_slashes=False)
def getData(self, request, serial_id):
logger.error('88888888888888888888888888888888888888888888888888888888888')
logger.error(serial_id)
return {'status': 'ok'}
I am sending request to the path:
requests.post('http://localhost:8000/v1/api/<serial_id>/data',
data= json.dumps({'data':'data'}),
params={'serial_id':1}
)
The problem is instead of printing 1 it print serial_id as <serial_id>.
Expected is:
88888888888888888888888888888888888888888888888888888888888
1
Actual is:
88888888888888888888888888888888888888888888888888888888888
<serial_id>
As #Md Jewele Islam states in the comments the url variable must be like:
url = 'http://localhost:8000/v1/api/{}/data'.format(str(serial_id))
and the request must be sent like:
import json
res = requests.post(url , data= json.dumps({'data':'data'}), params={'serial_id':1})
So you can print the response by:
print(res.text)
https://www.binance.com/restapipub.html
I have been trying to code a trading bot. I have figured out the data and decision making part of the program. Now I need to code the making order part of the program.
I checked their website and found that I need to supply the sha256 of
clientsecret|totalparams
and that
totalParams is defined as the query string concatenated with the request body
So far this is what I have:
import requests
headers = {
'X-MBX-APIKEY': MY_API_KEY,
}
data = [
('symbol', 'LTCBTC'),
('side', 'BUY'),
('type', 'LIMIT'),
('timeInForce', 'GTC'),
('quantity', '1'),
('price', '0.1'),
('recvWindow', '6000000'),
('timestamp', '1499827319559'),
('signature', NO_IDEA ),
]
requests.post('https://www.binance.com/api/v1/order', headers=headers, data=data)
I need to figure out what the signature and by extension totalparams would be.
The documentation just wants you to use the request body, the query string on the url, and the client secret together in one string (the query string and request body are concatenated together, and then the client secret is prepended with a | character).
You can use a prepared request; this gives you access to the query string and request body before sending:
import requests
import hashlib
from urllib.parse import urlparse
def calculate_signature(secret, data=None, params=None):
# the actual URL doesn't matter as this request is never sent.
request = requests.Request('POST', 'http://example.com',
data=data, params=params)
prepped = request.prepare()
query_string = urlparse(prepped.url).query
# neither the URL nor the body are encoded to bytes yet
total_params = query_string + prepped.body
return hashlib.sha256('{}|{}'.format(secret, total_params).encode('ASCII')).hexdigest()
MY_API_KEY = 'XXX'
CLIENT_SECRET = 'XXX'
headers = {
'X-MBX-APIKEY': MY_API_KEY,
}
data = [
('symbol', 'LTCBTC'),
('side', 'BUY'),
('type', 'LIMIT'),
('timeInForce', 'GTC'),
('quantity', '1'),
('price', '0.1'),
('recvWindow', '6000000'),
('timestamp', '1499827319559'),
]
data.append(
('signature', calculate_signature(CLIENT_SECRET, data=data)))
response = requests.post('https://www.binance.com/api/v1/order', data=data, headers=headers)
The prepared request object was only used to give you the request body to sign. Their API is a little convoluted in that they then expect you to append the signature to the request body itself rather than in a header (which is what most REST APIs do).
The calculate_signature() function produces the same results as the documentation:
>>> from urllib.parse import parse_qsl
>>> documentation_secret = 'NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j'
>>> requestBody = parse_qsl('symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=6000000×tamp=1499827319559')
>>> calculate_signature(documentation_secret, requestBody)
'24b39c6588d0f2378a2b641e68c00e87bc81d997146ca3c5482337857a045041'
>>> queryString = parse_qsl('symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC')
>>> requestBody = parse_qsl('quantity=1&price=0.1&recvWindow=6000000×tamp=1499827319559')
>>> calculate_signature(documentation_secret, requestBody, queryString)
'77eb3b3727bc8c523646e2a35f52a8eb4cc4418b24c113f3ea0b3b59248579d4'
hashlib provides various hash function including sha256, e.g.:
import hashlib
hashlib.sha256('|'.join([clientsecret, totalparams]).encode('utf-8')).hexdigest()
from the page you linked there is example of how to calculate it:
echo -n "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j|symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=6000000×tamp=1499827319559" | sha256sum
24b39c6588d0f2378a2b641e68c00e87bc81d997146ca3c5482337857a045041 -
simple function to calculate the sig without too many requests manipulations (because we know data is list of tuples it will be ordered and passed down correctly, if it was dict it might not preserve the order)
import hashlib
from urllib.parse import urlencode
data = [...] # your params
def calc_sig(data, your_secret_key):
sig_content = '%s|%s' % (your_secret_key, urlencode(data))
return hashlib.sha256(sig_content).hexdigest()
I'm trying to write a script that will search a string in google, loop and iterate the number in the string, and print the top links. I have this:
import urllib.parse
import urllib.request
import json as m_json
for x in range(3, 5):
query = '"Amazon Best Sellers Rank: #' + str(x) + ' in Kitchen & Dining": Amazon.com'
query = urllib.parse.urlencode ( { 'q' : query } )
response = urllib.request.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read().decode()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
title = result['title']
url = result['url'] # was URL in the original and that threw a name error exception
print ( title + '; ' + url )
I'm getting this error:
"TypeError: 'NoneType' object is not subscriptable" on line 10, results = ...
Same question Was posted by you beforetwo months and now again you are posting that Link to your question.
And by the way the answer to your question is also has been provided already on stackoverflow.
But again I am posting the code for you .
And using your code only I am getting the desired result in Python 2.7 .
import urllib
import json as m_json
for x in range(3, 5):
query = 'x mile run'
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
title = result['title']
url = result['url']
print ( title + '; ' + url )
I have written python wrapper for web interface here to print the result of online computation on the terminal. Input to this computation are given through the code.
The program I have done is:
import re
import mechanize
import sys
import os
import urlparse
def calc_relatedness():
br = mechanize.Browser()
br.open("http://ws4jdemo.appspot.com/")
br.select_form(nr = 0)
br["w1"] = "tree#n#01"
br["w2"] = "trunk#n#01"
response = br.submit()
print response
if __name__ == "__main__":
calc_relatedness()
and output of above program is:
<response_seek_wrapper at 0x1ef2878 whose wrapped object = <closeable_response at 0x1efe170 whose fp = <socket._fileobject object at 0x1e8cb50>>>
Can anyone tell me what is the meaning of this output?
Expected output from web computation is:
wup( tree#n#1 , trunk#n#1 ) = 0.4762
jcn( tree#n#1 , trunk#n#1 ) = 0.0706
lch( tree#n#1 , trunk#n#1 ) = 1.2040
lin( tree#n#1 , trunk#n#1 ) = 0.1620
res( tree#n#1 , trunk#n#1 ) = 1.3696
path( tree#n#1 , trunk#n#1 ) = 0.0833
lesk( tree#n#1 , trunk#n#1 ) = 1066
hso( tree#n#1 , trunk#n#1 ) = 4
The output that you see is actually the string representation of the response object, defined in its class. You see, response actually contains more than the response body itself, it has other information like headers and url, too. Based on this code, if you want to get the response body, you should change the last line in calc_relatedness() to:
print response.read()
You can get some information about response by calling response.info() and response.geturl().