我使用的是 ruby 版本 1.9.3,我喜欢从下面的视频 url 中获取主机名,
I'm using the ruby version 1.9.3, I like to get host name from the video url below,
我尝试过代码
require 'uri' url = "ferrari-view.4me.it/view-share/playerp/?plContext=ferrari-%201363948628-stream.4mecloud.it/live/ferrari/ngrp:livegenita/manifest.f4m&cartellaConfig=ferrari-4me.weebo.it/static/player/config/&cartellaLingua=ferrari-4me.weebo.it/static/player/config/&poster=pusher.newvision.it:8080/resources/img1.jpg&urlSkin=ferrari-4me.weebo.it/static/player/swf/skin.swf?a=1363014732171&method=GET&target_url=ferrari-4me.weebo.it/static/player/swf/player.swf&userLanguage=IT&styleTextColor=#000000&autoPlay=true&bufferTime=2&isLive=true&highlightColor=#eb2323&gaTrackerList=UA-23603234-4" puts URI.parse(url).host它抛出一个异常 URI::InvalidURIError: bad URI(is not URI?):
it throws an exception URI::InvalidURIError: bad URI(is not URI?):
我尝试对 URL 进行编码,然后像下面这样解析
I tried with encode the URL then parse like below
puts URI.parse(URI.parse(url)).host它抛出一个异常 URI::InvalidURIError: bad URI(is not URI?)
但以上代码适用于以下网址.
But above code works for the below URL.
url = www.youtube/v/GpQDa3PUAbU?version=3&autohide=1&autoplay=1
如何解决这个问题?请提出任何建议.谢谢
How to fix this? any suggestion please. Thanks
推荐答案此 url 无效,但它在浏览器中有效,因为浏览器本身对 :、/等特殊字符不那么严格
This url is not valid, but it works in browser because browser itself is less strict about special characters like :, /, etc.
您应该先对您的 URI 进行编码
You should encode your URI first
encoded_url = URI.encode(url)然后解析它
URI.parse(encoded_url)更多推荐
如何修复错误的 URI 不是 URI
发布评论