How to insert a variable in the middle of the address I pass to Urllib2?

Asked

Viewed 1,330 times

3

I’m using the function urllib2 in Python and need to put a variable in the middle of the link I pass to function. For example:

request = urllib2.Request('https://api.stackexchange.com/2.2/users/id_user?order=desc&sort=reputation&site=stackoverflow')

Where the id_user is a variable. How do I make him recognize the id_user as a variable?

  • Solved, url = "%s%d%"%("https://api.stackexchange.com/2.2/users/",id_user ,"?order=desc&sort=reputation&site=stackoverflow"). This way it recognizes in each interval before, between and after the commas where each argument will be predefined. String comma integer comma string. Thanks to everyone who tried to help.

  • Mr Sunstreaker, take a right look at the question, you will notice that there is a "user_id" in the middle of the link, and shortly after I left explanatory "Where id_user is a variable." Thank you very much for your attention.

2 answers

3

There are some ways to do this.

Operator modulo %:

id_User = "IdUserNameAqui"
request = urllib2.Request('https://api.stackexchange.com/2.2/users/%s?order=desc&sort=reputation&site=stackoverflow' % id_User)

Operator unário +:

id_User = "IdUserNameAqui"
request = urllib2.Request('https://api.stackexchange.com/2.2/users/' + str(id_User) + '?order=desc&sort=reputation&site=stackoverflow')

Function str.format():

id_User = "IdUserNameAqui"
request = urllib2.Request("https://api.stackexchange.com/2.2/users/{0}?order=desc&sort=reputation&site=stackoverflow".format(id_User))

Example

The example below will insert the contents of a variable in the middle of the URL, make the request and get some information from JSON returned.

import requests
#Para instalar: sudo pip install requests

id_User = "1"
url = "https://api.stackexchange.com/2.2/users/{0}?order=desc&sort=reputation&site=stackoverflow".format(id_User)
json = requests.get(url).json()

accountID  = json['items'][0]['account_id']
websiteURL = json['items'][0]['website_url']
userName   = json['items'][0]['display_name']

print ("ID: {0}\nNome de exibicao: {1}\nSite: {2}\n".format(accountID, userName, websiteURL))

# Saída
# ID: 1
# Nome de exibicao: Jeff Atwood
# Site: http://www.codinghorror.com/blog/

0

Use the method format().

id_user = 1
url = 'https://api.stackexchange.com/2.2/users/{0}?order=desc&sort=reputation&site=stackoverflow'
request = urllib2.Request(url.format(id_user))

You can use %, this way:

url = "...users/%s?order..." % id_user

It will work, but always prefer the format() method. As described by documentation official, it handles common concatenation errors, and will be the official method used by Python from now on. The idea is that soon the % will no longer be accepted.

You can also use "plus Operation":

url = '...user/' + id_user + '?order...'

But it’s bad practice. When the code gets bigger (and you should always think about it), this url will be unreadable. If you need to join two strings quickly, prefer to use Join().

url = ''.join(['...user/',
               id_user,
               '?order...'])

In short, use the format(). It’s what Python asks you to use, after all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.