there's lots of information on retrieving GET variables from a python script. Unfortunately I can't figure out how to send GET variables from a python script to an HTML page. So I'm just wondering if there's a simple way to do this.
I'm using Google App Engine webapp to develop my site. Thanks for your support!
Just append the get parameters to the url: request.html?param1=value1¶m2=value2.
Now you could just create your string with some python variables which would hold the param names and values.
Edit: better use python's url lib:
import urllib
params = urllib.urlencode({'param1': 'value1', 'param2': 'value2', 'value3': 'param3'})
url = "example.com?%s" % params
Related
When using python and steam api trying to get a certain value using data = profile['gameextrainfo'] profile has the value of the API which is.
d = {"response":
{"players":[
{"steamid":"76561199446676130",
"communityvisibilitystate":3,
"profilestate":1,
"personaname":"S7 WatchDog",
"profileurl":"https://steamcommunity.com/profiles/76561199446676130/",
"avatar":"https://avatars.akamai.steamstatic.com/415bd0e2ddd5d8e99309eec6d7a2566cbb09022d.jpg","avatarmedium":"https://avatars.akamai.steamstatic.com/415bd0e2ddd5d8e99309eec6d7a2566cbb09022d_medium.jpg","avatarfull":"https://avatars.akamai.steamstatic.com/415bd0e2ddd5d8e99309eec6d7a2566cbb09022d_full.jpg",
"avatarhash":"415bd0e2ddd5d8e99309eec6d7a2566cbb09022d",
"personastate":1,
"primaryclanid":"103582791429521408",
"timecreated":1671522419,
"personastateflags":0,
"gameextrainfo":"Counter-Strike: Global Offensive",
"gameid":"730"}]
}
}
I cannot seem to filter out any key. I've tried all of them and python just fails to find them. Any ideas
Tried all keys. Tried using requests python module
You could use the json module to parse the response and access the individual data points. For example:
import json
data = json.loads(profile)
gameextrainfo = data['response']['players'][0]['gameextrainfo']
print(gameextrainfo)
So I understand the concept of sending payloads with requests, but I am struggling to understand how to know what to send.
for example, payload = {'key1': 'value1', 'key2': 'value2'} is the payload,
but how do I find what key1 is? Is it the element ID? I have worked closely to selenium and I am looking at requests at a faster alternative.
Thanks for your help, Jake.
Requests is a library which allows you to fetch URL's using GET, POST, PUT, etc requests. On some of those you can add a payload, or extra data which the server uses to do something.
So the payload is handled by the server, but you have to supply them which you do by defining that dictionary. This is all dependent on what the server expects to receive. I am assuming you read the following explanation:
Source
You often want to send some sort of data in the URL’s query string. If
you were constructing the URL by hand, this data would be given as
key/value pairs in the URL after a question mark, e.g.
httpbin.org/get?key=val. Requests allows you to provide these
arguments as a dictionary of strings, using the params keyword
argument. As an example, if you wanted to pass key1=value1 and
key2=value2 to httpbin.org/get, you would use the following code:
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get('https://httpbin.org/get', params=payload)
The payload here is a variable which defines the parameters of a request.
i fetch all the detail from the desire website but unable to get the some specific information please guide me for that.
targeted domain: https://shop.adidas.ae/en/messi-16-3-indoor-boots/BA9855.html
my code isresponse.xpath('//ul[#class="product-size"]//li/text()').extract()
need to fetch data!!!
Thanks!
Often ecommerce websites have data in json format in page source and then have javscript unpack it on users end.
In this case you can open up the page source with javascript disabled and search for keywords (like specific size).
I found in this case it can be found with regular expressions:
import re
import json
data = re.findall('window.assets.sizesMap = (\{.+?\});', response.body_as_unicode())
json.loads(data[0])
Out:
{'16': {'uk': '0k', 'us': '0.5'},
'17': {'uk': '1k', 'us': '1'},
'18': {'uk': '2k', 'us': '2.5'},
...}
Edit: More accurately you probably want to get different part of the json but nevertheless the answer is more or less the same:
data = re.findall('window.assets.sizes = (\{(?:.|\n)+?\});', response.body_as_unicode())
json.loads(data[0].replace("'", '"')) # replace single quotes to doubles
The data you want to fetch is loaded from a javascript. It is said explicitly in the tag class="js-size-value ".
If you want to get it, you will need to use a rendering service. I suggest you use Splash, it is simple to install and simple to use. You will need docker to install splash.
So I am using python Requests( http://docs.python-requests.org/en/latest/) to work with a REST api. My question is when using Requests what data field actually appends info to the url for GET requests? I wasn't sure if if I could use "payload" or if "params" does it.
It's all in the docs:
http://docs.python-requests.org/en/latest/user/quickstart/#passing-parameters-in-urls
As an example, if you wanted to pass key1=value1 and key2=value2 to
httpbin.org/get, you would use the following code:
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
After all - you can of course easily test it, in case you're simply not sure which of two choices is the proper one.
As the example shows, http://httpbin.org (a site by the requests author) is a nice way to test any web programming you're doing.
I'm starting to code in python using pysimplesoap. Testing first against a service available on Internet. I'm stuck trying to parse the result of the Soap query.
I coded:
#!/usr/bin/python
from pysimplesoap.client import SoapClient
import pysimplesoap
import logging
logging.basicConfig()
client=SoapClient(wsdl="http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?wsdl",trace=True)
response = client.VerifyEmail(email="a-valid-gmail-address#gmail.com",LicenseKey="?")
print response
I get the following which means the Soap request was positive:
{'VerifyEmailResult': {'GoodEmail': True, 'LastMailServer': u'gmail-smtp-in.l.google.com', 'ResponseText': u'Mail Server will accept email', 'ResponseCode': 3}}
I now want to extract the value of GoodEmail which is equal to True from "response" and store it in a variable named "result".
I tried various things, without success.
I must admit I'm very new to Python, and would appreciate help from a knowledgeable person!
What you get as the response is a Python dict. You can access the GoodEmail value like that:
result = response['VerifyEmailResult']['GoodEmail']
You can read more about Python data types here: http://docs.python.org/2/library/stdtypes.html