简体   繁体   中英

Python: How to combine a string and a list

I am currently trying to connect an URL (String) to a number (list) to form a new URL. The code looks like this:

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)

Now, the new next_page_url looks like this:

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']

Basically, it added the list to the URL. But I don't need the brackets and the quotation marks ['10']. I just need the 10.

How to add the two variables without adding them?

Replace next_page_number = str(next_page_number) with this:

next_page_number = next_page_number[0]

This works because next_page_number is a list and next_page_number[0] will select the first item of the list which is the string '10' . When you did str(next_page_number) , you simply make a string representation of the list which will include the brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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