modify url address in python - python

I'm trying to add strings into a URL address in order to get data from a server.
the string depends on a user input. the user input i saved under a variable called id.
id = str(raw_input("Enter a valid ID: "))
my url address looks like this:
url = "http://www.test.com/?%s&%s" % (id, api_key)
when i'm printing the URL just to check I've got everything in order i get this result:
http://www.test.com/?<built-in function id>&ef50250
I followed some other questions and some other tutorials but none seem to clearly it for my.
It is my first project so excuse if i ask any obvious questions.

id is a built-in function. Give a different name to your variable. By the way, raw_input returns str. So you can get rid of str(raw_input(...))
>>> my_id = raw_input("Enter a valid ID: ")
Enter a valid ID: 12
>>> api_key='abc'
>>> url = "http://www.test.com/?%s&%s" % (my_id, api_key)
>>> print url
http://www.test.com/?12&abc

Related

How to call an API but only grab one specific piece of info in Python

I want to create a simple python program that calls the colornames.org API for the name of any given hex code inputted by the user. However, all I want my program to output is the "name" info.
How can I make it only output that and not all of the information?
Code below:
import requests
import json
hexcodeinput = input("Hex code you've found (format: FF0000, no #): ")
print(hexcodeinput + " is your selected hex code. Searching...")
response = requests.get("https://colornames.org/search/json/?hex=" + (hexcodeinput))
print(response.text)
You should get the response as json, not as plain text. Then it is a dict which you can use:
import requests
hex_code = input("Hex code you've found (format: FF0000, no #): ")
print("%s is your selected hex code. Searching..." % hex_code)
response = requests.get("https://colornames.org/search/json/?hex="+hex_code)
details = response.json() # a dict with all the info
print(details['name']) # get the name from that dict

How to find a string among many strings in a line

I have a requirement to find the user from the log.
I have a line of code from my log file. One of the strings in the line is a userid. I have the list of all userid also.
Is there any easy way to identify the userid mentioned in the line.
Eg: Calling Business function ProcessSOMBFCommitment_Sourcing from F4211FSEditLine for ND9074524. Application Name [P421002], Version [] (BSFNLevel = 3)
Here, ND9074525 is the user id. My intention is to identify the user from the line.
Other possible userid can be AB9074158, AC9074168, AD9074123, AE9074152
I do not want to loop through all the possible userid. I thought of creating a list of all userid's and find the userid used in line by some method. Not sure if it exists.
You can use this regex to fetch the user id:
[A-Z]{2}\d{7}
And then check against valid user ids.
Regex Demonstration
Code:
import re
users = ['AB9074158', 'AC9074168', 'AD9074123', 'AE9074152']
s = 'Calling Business function ProcessSOMBFCommitment_Sourcing from F4211FSEditLine for ND9074524. Application Name [P421002], Version [] (BSFNLevel = 3)'
pat = '[A-Z]{2}\d{7}'
user_id = re.search(pat, s).group(0)
print(user_id)
if user_id in users:
print("User accepted!")
else:
print("User not accepted")
Output:
'ND9074524'
'User not accepted'
Use a regex to find all strings that match the pattern of a userid; then you can see if any of them are actual userids.

How To Get The value of the input by passing a dynamic input name using Xpath?

The working code is like this:
csrf = list(set(htmls.xpath("//input[#name='whatever']/#value")))[0]
However, I'm trying to get that input name as a parameter passed into the function, in that way I would do something like this:
tokenname = sys.argv[2]
which gives the value 'whatever', and I want to pass it something like this:
csrf = list(set(htmls.xpath("//input[#name="+tokenname+"]/#value")))[0]
But it doesn't work that way, anyway to pass a variable in that #name value?
The full code is here:
import requests
from lxml import html
import json
import sys
session_requests = requests.session()
login_url = sys.argv[1]
tokenname = sys.argv[2]
result = session_requests.get(login_url)
htmls = html.fromstring(result.text)
csrf = list(set(htmls.xpath("//input[#name={}]/#value".format(tokenname))))[0]
print csrf
EDIT
Based upon discussion, looks like you had issues with " and escape charcaters.
Use following
csrf = list(set(htmls.xpath("//input[#name=\"{}\"]/#value".format(tokenname))))[0]
Old
You can use format as below
"//input[#name={}]/#value".format('whatever')
From python doc site
str.format(*args, **kwargs)
Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.
>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'

python check if list items is in string

link = 'http://dedegood.com'
wrongdomain = ['google','facebook','twitter']
if any(link.find(i) for i in wrongdomain):
print 'pass this url'
else:
print 'good'
I want to check if link contains the words in wrongdomain
Why this always print 'pass this url'?
link has no google or facebook or twitter in it
I try seperate like link.find('google')
it will return -1 .so what's the problem?
Please help me to check my logic.Thank you
bool(-1) is True in Python. Instead of find, you can just do:
if any(domain in link for domain in wrongdomain):
Just remember that will also match the rest of the url, not just the domain.
Your method will not work correctly like a url like http://dedegood.com/google this. So you can use something like;
link = 'http://dedegood.com'
wrongdomain = ['google','facebook','twitter']
a=link.split("//")
b=a[1].split(".")
if any(domain in b[0] for domain in wrongdomain):
print ('pass this url')
else:
print ('good')
Since you just want to check url, you can use this one. Instead of checking all link, it's checking only the name of website. So if any url like http://dedegood.com/google will not be a problem.
Do you want to know whether the url's domain is in wrongdomain or not? I would suggest you can do this for better performance:
import urlparse
import tldextract
link = 'http://dedegood.com'
wrongdomain = ['google','facebook','twitter']
parsed = tldextract.extract(link)
if parsed.domain in wrongdomain:
print 'pass this url'
else:
print 'good'
You could check out tldextract, a library designed to get domain from a url.

How can I extract an HTML form value?

Hi I have the following in a page:
input id="cu_first_name" class="input_text" type="text" value="test_name" name="cu_name"
I am trying to extract the value and print it in python.
I use:
username = driver.find_element_by_id("cu_first_name")
print username.text
But this will not work since there is no actual text there, I need the "test_name" to be printed, pls help me!
You've gotten the element by id. From there, you need to get the element's attribute. Give the following a try:
username = driver.find_element_by_id("cu_first_name")
value = username.get_attribute('value')
print value

Categories