【问题标题】:Python: How to combine a string and a listPython:如何组合字符串和列表
【发布时间】:2020-01-16 04:04:44
【问题描述】:

我目前正在尝试将 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 个。

两个变量不加怎么加?

【问题讨论】:

    标签: python regex list web-scraping


    【解决方案1】:

    用这个替换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) 时,您只需创建一个包含括号的列表的字符串表示形式。

    【讨论】:

      猜你喜欢
      • 2012-01-07
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多