Passing a variable to a URL?

So I'm new to python and I desperately need help.

I have a file that contains a bunch of identifiers (integer values) written in em. Its a text file.

Now I need to pass each identifier inside the file to the url.

For example, "https://example.com/en")

It will be done this way.

A = json.load(urllib.urlopen("https://example.com/(the first id present in the text file)")) print A 

What it will essentially be is that he will read certain information about the identifier present in the above URL and show it. I want this to work in a loop format, where it will read all the identifiers inside the text file and pass it to the URL specified in "A" and constantly display the values. Is there any way to do this?

I would really appreciate it if anyone could help me!

+7
source share
4 answers

Static style concatenation can be used.

 >>> id = "3333333" >>> url = "https://example.com/%s" % id >>> print url https://example.com/3333333 >>> 

New style string formatting:

 >>> url = "https://example.com/{0}".format(id) >>> print url https://example.com/3333333 >>> 

Reading the file specified in avasal with a little change:

 f = open('file.txt', 'r') for line in f.readlines(): id = line.strip('\n') url = "https://example.com/{0}".format(id) urlobj = urllib.urlopen(url) try: json_data = json.loads(urlobj) print json_data except: print urlobj.readlines() 
+17
source

lazy style:

 url = "https://example.com/" + first_id A = json.load(urllib.urlopen(url)) print A 

old style:

 url = "https://example.com/%s" % first_id A = json.load(urllib.urlopen(url)) print A 

New style 2.6+:

 url = "https://example.com/{0}".format( first_id ) A = json.load(urllib.urlopen(url)) print A 

new style 2.7+:

 url = "https://example.com/{}".format( first_id ) A = json.load(urllib.urlopen(url)) print A 
+5
source

The first thing you need to do is know how to read each line from the file. You must open the file first; you can do this with the with statement:

 with open('my-file-name.txt') as intfile: 

This opens the file and saves the link to that file in the intfile , and it automatically closes the file at the end of your with block. Then you need to read each line from the file; you can do this with the usual old for loop:

  for line in intfile: 

This will go through each line in the file, reading them one at a time. In your loop, you can access each line as line . It remains only to make a request to your site using the code that you specified. One bit of your missing is what is called β€œstring interpolation," which allows you to format a string with other strings, numbers, or anything else. In your case, you want to put a line (a line from your file) inside another line (URL). To do this, you use the %s flag along with the string interpolation operator, % :

 url = 'http://example.com/?id=%s' % line A = json.load(urllib.urlopen(url)) print A 

Combining all this, you get:

 with open('my-file-name.txt') as intfile: for line in intfile: url = 'http://example.com/?id=%s' % line A = json.load(urllib.urlopen(url)) print A 
+2
source

Python 3+

Newline formatting is supported in Python 3, which is a more readable and better way to format a string. Here is a good article on the same: Python 3 f-Strings

In this case, it can be formatted as

url = f"https://example.com/{id}"

Detailed example

If you want to pass several parameters to the URL, you can do this as shown below.

 name = "test_api_4" owner = " jainik@socure.com " url = f"http://localhost:5001/files/create" \ f"?name={name}" \ f"&owner={owner}" \ 

Here we use several f-lines, and you can add '\' to them. This will keep them on the same line without inserting a newline between them.

For values ​​that take place

For these values, you must import from urllib.parse import quote into your Python file, and then quote a line like: quote("firstname lastname")

This will replace the space character with %20 .

0
source

All Articles