将带有空格的字符串转换为 URL

发布于 2024-09-07 08:37:46 字数 851 浏览 4 评论 0原文

我正在使用 ruby​​ 和谷歌反向地理编码 yql 表来理想地自动化我的一些搜索查询。我遇到的问题是将查询转换为合法的 url 格式。问题是我使用的编码返回非法网址。我正在运行的查询如下所示

query="select * from google.geocoding where q='40.714224,-73.961452'" 
pQuery= CGI::escape(query)

处理后的查询的最终输出看起来像这样

http://query.yahooapis.com/v1/public/yql?q=select+%2A+from+google.geocoding+where+q%3D%2740.3714224%2C--73.961452%27+format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

唉,网址是非法的。当检查 YQL 控制台中的查询应是什么样子时,我得到以下信息

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.geocoding%20where%20q%3D%2240.714224%2C-73.961452%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

正如您希望看到的:),编码全部错误。我想知道有谁知道我如何生成正确的网址。

I'm using ruby and googles reverse geocode yql table to ideally automate some search query I have. The problem I hit is turning the query into a legal url format. The issue is that the encoding I'm using is returning illegal urls. The query I'm running is as follows

query="select * from google.geocoding where q='40.714224,-73.961452'" 
pQuery= CGI::escape(query)

The eventual output for the processed query looks like this

http://query.yahooapis.com/v1/public/yql?q=select+%2A+from+google.geocoding+where+q%3D%2740.3714224%2C--73.961452%27+format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

Alas the url is illegal. When checking what the query shoud look like in the YQL console I get the following

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.geocoding%20where%20q%3D%2240.714224%2C-73.961452%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

As you can hopefully see :), the encoding is all wrong. I was wondering does anyone know how I can go about generating correct urls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2024-09-14 08:37:46

如果你想转义 URI,你应该使用 URI::escape

require 'uri'

URI.escape("select * from google.geocoding where q='40.714224,-73.961452'")
# => "select%20*%20from%20google.geocoding%20where%20q='40.714224,-73.961452'"

If you want to escape a URI, you should use URI::escape:

require 'uri'

URI.escape("select * from google.geocoding where q='40.714224,-73.961452'")
# => "select%20*%20from%20google.geocoding%20where%20q='40.714224,-73.961452'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文