Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

311
Visualizações
What is the pythonic way of building full urls for links?

I'm looking for a way to build urls in python3 without having to do string concatenation. I get that I can

import requests
url_endpoint = 'https://www.duckduckgo.com'
mydict = {'q': 'whee! Stanford!!!', 'something': 'else'}
resp = requests.get(url_endpoint, params=mydict)
print(resp.url) # THIS IS EXACTLY WHAT I WANT

or

from requests import Request, Session
s = Session()
req = Request('GET',  url, params={'q': 'blah'})
print(req.url)
# I didn't get this to work, but from the docs 
#   it should build the url without making the call

or

url = baseurl + "?" + urllib.urlencode(params)

I like that the request library intelligently decides to drop ? if it isn't needed, but that code actually makes a full GET request so instead of just building a full text url (which I plan to dump to an html tag). I am using django, but I didn't see anything to help with that in the core library.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Django comes with QueryDicts which basically do everything you want.

def make_url(url, args=None):
    query = QueryDict(mutable=True)
    query.update(args or {})
    return '{}{}{}'.format(url, '?' if query else '', query.urlencode())

It supports multiple values per argument just like you can encounter in a url: example.com/foo?a=1&a=2&a=3.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda