【问题标题】:How to Extract URL content using Rails or Javascript如何使用 Rails 或 Javascript 提取 URL 内容
【发布时间】:2017-07-31 20:27:24
【问题描述】:

我有这个问题很容易将内容添加到 url 但如何提取它

http://localhost:3000/pages/wish?url=file:///Users/ruelnopal/Desktop/sitetest/index.html&image=http://zalora-media-live-ph.s3.amazonaws.com/product/89/6948/1.jpg&price=PHP%C2%A0799.00&desc=Polo%20shirt%20with%20contrast%20tip,%20Collared%20neckline,%20Unlined,%20Regular%20fit&title=Pique%20Tipping%20Polo%20Shirt&display=popup

我正在尝试提取 url图片

【问题讨论】:

    标签: javascript ruby-on-rails json


    【解决方案1】:

    这是一个简单的实现:

    const queryString = location.search.substring(1) //the part after the "?"
    const queryParams = queryString.split('&')
    //Creates a mapping of params to values
    const query = new Map(queryParams.map(param => param.split('=')))
    console.log(query.get('url')) //"file:///Users/ruelnopal/Desktop/sitetest/index.html"
    console.log(query.get('image')) //"http://zalora-media-live-ph.s3.amazonaws.com/product/89/6948/1.jpg"
    

    【讨论】:

      【解决方案2】:

      这是一个返回 URL 中所有参数的函数

      function getUrlParams(url) {
      
        // get query string from url (optional) or window
        var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
      
        // we'll store the parameters here
        var obj = {};
      
        // if query string exists
        if (queryString) {
      
          // stuff after # is not part of query string, so get rid of it
          queryString = queryString.split('#')[0];
      
          // split our query string into its component parts
          var arr = queryString.split('&');
      
          for (var i=0; i<arr.length; i++) {
            // separate the keys and the values
            var a = arr[i].split('=');
      
            // in case params look like: list[]=thing1&list[]=thing2
            var paramNum = undefined;
            var paramName = a[0].replace(/\[\d*\]/, function(v) {
              paramNum = v.slice(1,-1);
              return '';
            });
      
            // set parameter value (use 'true' if empty)
            var paramValue = typeof(a[1])==='undefined' ? true : a[1];
      
            // (optional) keep case consistent
            paramName = paramName.toLowerCase();
            paramValue = paramValue.toLowerCase();
      
            // if parameter name already exists
            if (obj[paramName]) {
              // convert value to array (if still string)
              if (typeof obj[paramName] === 'string') {
                obj[paramName] = [obj[paramName]];
              }
              // if no array index number specified...
              if (typeof paramNum === 'undefined') {
                // put the value on the end of the array
                obj[paramName].push(paramValue);
              }
              // if array index number specified...
              else {
                // put the value at that index number
                obj[paramName][paramNum] = paramValue;
              }
            }
            // if param name doesn't exist yet, set it
            else {
              obj[paramName] = paramValue;
            }
          }
        }
      
        return obj;
      }
      

      【讨论】:

        【解决方案3】:

        尝试使用OpenURI 模块

        require "open-uri"
        
        File.open('my_image.png', 'wb') do |fo|
            fo.write open("https://upload.wikimedia.org/wikipedia/commons/5/53/Wikipedia-logo-en-big.png").read
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-30
          • 1970-01-01
          • 2020-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-14
          • 2013-01-18
          相关资源
          最近更新 更多