【问题标题】:How to concatenate url + string + value ? ruby如何连接 url + 字符串 + 值?红宝石
【发布时间】:2023-01-08 01:37:48
【问题描述】:

试图解析产品类别中的所有页面。我想使用 original_url+'&p='+value 检查每个页面,其中 value 是页数。

How to write this properly full_source = url + "&p=" + "#{number_of_page}" ``

【问题讨论】:

    标签: ruby


    【解决方案1】:

    我想你想要

    full_source = "#{url}?p=#{number_of_page}"
    

    你的问题有一个符号:

    full_source = "#{url}&p=#{number_of_page}"
    

    一个更好的方法是:

    uri = URI.parse(url)
    uri.query = URI.encode_www_form({ p: number_of_page })
    puts uri.to_s # URL with query params
    

    这是一个例子:

    url = 'http://google.com/search'
    uri = URI.parse(url)
    uri.query = URI.encode_www_form({ q: 'ruby' })
    puts uri.to_s
    # => "http://google.com/search?q=ruby""
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2014-02-11
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多