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)
2
  • %s expects string but 324093002 is number - did you try str(i) ? Commented Dec 8, 2020 at 12:49
  • 1
    Does this answer your question? Add params to given URL in Python Commented Dec 8, 2020 at 12:49

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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'

2 Comments

your i and b are backward in the format()
Also drop the * around the {} to match the desired string. might as well change the first URL too.
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"

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.