add-semi-colons
add-semi-colons

Reputation: 18840

Python: Concatenate URL and Integer

I am trying to concatenate url and integer base on the iteration of the loop. But I am getting error: TypeError: not all arguments converted during string formatting

for i in range(0,20):
    convertedI = str(i)
    address = 'http://www.google.com/search?q=%s&num=100&hl=en&start='.join(convertedI) % (urllib.quote_plus(query))

I also tried to urllib.urlencode function but ended up getting the same error.

I guess I should have mentioned in addition to query which is a string that I pass it in in my main I want assign current iteration value to the start parameter in the url > &start=1

so first iteration I would like to have my url as

http://www.google.com/search?q=%s&num=100&hl=en&start=1' % (urllib.quote_plus(query))

Upvotes: 1

Views: 3141

Answers (2)

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56438

for i in range(0, 20):
    address = 'http://www.google.com/search?q=%s&num=100&hl=en&start=%s' % (urllib.quote_plus(query), i)

No need to use str as %s implicitly does that for you. Nor is there a need to join, as join is used to take multiple strings within a sequence and pull them together with a join character. You just want simple string formatting.

Upvotes: 3

BrenBarn
BrenBarn

Reputation: 251618

I think you are misunderstanding what join does. It uses its argument as a delimiter to join the elements of a passed sequence:

>>> ','.join(['a', 'b', 'c'])
'a,b,c'

It sounds like you just want to do "http://..." + convertedI, but it's not clear exactly what you're trying to do. Where do you want convertedI and the urllib.quote_plus(query) value to go in the string?

Upvotes: 1

Related Questions