繁体   English   中英

Python:如何组合字符串和列表

[英]Python: How to combine a string and a list

我目前正在尝试将URL(字符串)连接到数字(列表)以形成新的URL。 代码如下:

next_page_number = response.xpath('//*[@class="btn btn-danger"]/@onclick').re('-?\d+')
next_page_number = str(next_page_number)

url = "https://blogabet.com/tipsters/?f[language]=all&f[pickType]=all&f[sport]=all&f[sportPercent]=&f[leagues]=all&f[picksOver]=0&f[lastActive]=12&f[bookiesUsed]=null&f[bookiePercent]=&f[order]=followers&f[start]="

next_page_url= (url + next_page_number)
print (next_page_url)

现在,新的next_page_url如下所示:

https://blogabet.com/tipsters/?f[language]=all&f[pickType]=all&f[sport]=all&f[sportPercent]=&f[leagues]=all&f[picksOver]=0&f[lastActive]=12&f[bookiesUsed]=null&f[bookiePercent]=&f[order]=followers&f[start]=['10']

基本上,它将列表添加到URL。 但是我不需要方括号和引号['10']。 我只需要10。

如何添加两个变量而不添加它们?

next_page_number = str(next_page_number)替换next_page_number = str(next_page_number)

next_page_number = next_page_number[0]

之所以有效,是因为next_page_number是一个列表,并且next_page_number[0]将选择列表的第一项,即字符串'10' 完成str(next_page_number) ,您只需对列表进行字符串表示即可,其中将包括方括号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM