2

I can format the link (link1) with 1 variable however when I want to format the link (link2) with 2 variables, it throws an error ( ValueError: unsupported format character 'A' (0x41) at index 94) The link2 is supposed to be : https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=Topper&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F324093002%2F

is there any solution for this ?

i = 324093002
b = "Topper"
link_product = 'https://www.real.de/product/%s/'
link_keyword = 'https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=*%s*&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F*%s*%2F'
link1 = link_product %i
link2 = link_keyword % (i, b)
mht
  • 133
  • 1
  • 2
  • 9
  • `%s` expects string but `324093002` is number - did you try `str(i)` ? – furas Dec 08 '20 at 12:49
  • 1
    Does this answer your question? [Add params to given URL in Python](https://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python) – Maurice Meyer Dec 08 '20 at 12:49

4 Answers4

2

Char % has special meaning in string formatting - like %s, %i, etc.- and you have %3A %2F which are not correct formatting. You have to use %% to inform Python that you need normal char % - %%3A %%2F

i = 324093002
b = "Topper"
link_keyword = 'https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=*%s*&origin=https%%3A%%2F%%2Fwww.real.de%%2Fproduct%%2F*%s*%%2F'

link2 = link_keyword % (i, b)
print(link2)
furas
  • 134,197
  • 12
  • 106
  • 148
1

I would suggest to use f-string formatting (> Python 3.6). See here for more info.

i = 324093002
b = "Topper"
link_product = f"https://www.real.de/product/{i}/"
link_keyword = f"https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=*{i}*&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F*{b}*%2F"
YoniPrv
  • 73
  • 5
1

You should make use of urllib, to prevent mixing data with an already urlencoded URL:

from urllib.parse import urlencode, urljoin, urlparse

i = 324093002
b = "Topper"
link_product = urljoin('https://www.real.de/product/', str(i))

link_url = 'https://widget.s24.com/applications/1e082c83/widgets/422/products?{}'
link_params = {'searchTerm': i, 'origin': link_product}

link_keyword = link_url.format(urlencode(link_params))
print(link_keyword)

Out:

https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=324093002&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F324093002

Note:
Especially when b changes into a term that uses non URL safe characters, like Top top or iPhone7+ things get messed up using regular string formatting.

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
0

Consider using the format() method:

i = 324093002
b = "Topper"
link_product = 'https://www.real.de/product/{}/'
link_keyword = 'https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm={}&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F{}%2F'

link1 = link_product.format(i)
link2 = link_keyword.format(b, i)
print(link2)


'https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=Topper&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F324093002%2F'
Z Li
  • 4,133
  • 1
  • 4
  • 19