1

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))

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't use i anywhere in the URL, which the question clearly intended to.
@DavidRobinson yup, the original construction confused me, I assume he means to concatenate i to the end so I've updated my answer
I have edited the question to be more clear. Sorry for any confusion.
1

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?

2 Comments

I tried this but I am getting TypeError: not all arguments converted during string formatting address = 'google.com/search?q=%s&num=100&hl=en&start=' + convertedI % (urllib.quote_plus(query))
Why did you put a semicolon in there? As you have it, you'd have to put parentheses around the url and convertedl (order of operations would otherwise try and add the query to the convertedl string). Daniel DiPaulo's answer is best, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.