Mike Batowski
Mike Batowski

Reputation: 15

My code gives a "TypeError: not all arguments converted during string formatting" what's wrong?

This is the code:

    from bs4 import BeautifulSoup
    import requests

    x = 0
    value = 1
    x = x + value
    url = "https://www.bol.com/nl/s/algemeen/zoekresultaten/sc/media_all/index.html?" + "page=%s" + "&searchtext=ipad" % x
    response = requests.get(url)
    html = response.text

    soup = BeautifulSoup(html, "lxml")
    letters = soup.find_all("a", "product-title")

    for a in letters:
        title = a.get_text()
        print(title)

It gives me this error:

line 7, in url = "https://www.bol.com/nl/s/algemeen/zoekresultaten/sc/media_all/index.html?" + "page=%s" + "&searchtext=ipad" % x TypeError: not all arguments converted during string formatting

I want with this line of code to scrape all the webpages.

Upvotes: 0

Views: 92

Answers (2)

Lgiro
Lgiro

Reputation: 772

The string interpolation is being applied to the wrong string (the interpolation happens before the concatenation).

To fix this, change it to:

"https://www.bol.com/nl/s/algemeen/zoekresultaten/sc/media_all/index.html?" + "page=%s" % x + "&searchtext=ipad"

Upvotes: 3

Sumner Evans
Sumner Evans

Reputation: 9157

Another way of doing it is to wrap the string in parentheses and then do string concatenation on that:

url = ("https://www.bol.com/nl/s/algemeen/zoekresultaten/sc/media_all/index.html?" +
       "page=%s" +
       "&searchtext=ipad") % x

One advantage of this is that you can take advantage of implied line continuation for long lines using parentheses.

Upvotes: 0

Related Questions