Python:连接URL和Integ

2024-10-01 17:34:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在循环迭代的基础上连接url和integer基。但是我得到了错误: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))

我也试过urllib.urlencode但最后得到了同样的错误。在

我想除了在main中传入的一个字符串query之外,我还应该提到我想要将当前迭代值赋给url中的start参数&start=1

所以第一次迭代我希望我的网址

^{pr2}$

Tags: urlstring错误notintegerallurllibquery
2条回答

我想你误解了join的作用。它使用其参数作为分隔符连接传递序列的元素:

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

听起来你只是想做"http://..." + convertedI,但并不清楚你到底想做什么。您希望convertedIurllib.quote_plus(query)值在字符串中的哪个位置?

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)

不需要使用str,因为%s隐式地为您做这件事。也不需要join,因为join用于在一个序列中获取多个字符串,并使用连接字符将它们拉到一起。你只需要简单的字符串格式。

相关问题 更多 >

    热门问题