机械化的运用
我想从接受简单输入的网站获得响应,这也反映在 url 的参数中。是使用常规方法简单地获取结果更好,例如带有一些参数集的 OpenURI.open_uri(...)
,还是使用 mechanize、提取表单并获取结果更好通过提交结果?
mechanize 页面给出了提取表单并提交以从 Google 搜索获取搜索结果的示例。不过,这一切都可以简单地通过 OpenURI.open_uri("http://www.google.com/search?q=...").read
完成。我有什么理由应该尝试使用其中一种方式吗?
I want to get response from websites that take a simple input, which is also reflected in the parameter of the url. Is it better to simply get the result by using conventional methods, for example OpenURI.open_uri(...)
with some parameter set, or it is better to use mechanize, extract the form, and get the result through submit?
The mechanize page gives an example of extracting a form and submitting it to get the search result from Google search. However, this much can be done simply as OpenURI.open_uri("http://www.google.com/search?q=...").read
. Is there any reason I should try to use one way or the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多网站被证明是最容易使用机械化的。如果您需要登录并在访问数据之前设置 cookie,那么 mechanize 是一种简单的方法。同样,如果有很多隐藏字段需要匹配(例如 CSRF 令牌),那么使用 mechanize 获取页面,然后在填写数据的情况下提交它通常是一种更简单的方法,您可以自己制作 URL。
如果它是一个简单的 URI,比如 google 的搜索页面,那么手动构建它可能会更简单。
There are lots of sites where it turns out to be easiest to use mechanize. If you need to log in, and set a cookie before accessing the data, then mechanize is a simple way of doing this. Similarly, if there are lots of hidden fields that need to be matched (such as CSRF token), then fetching the page using mechanize then submitting it with the data filled out is often a more foolproof method that crafting the URL yourself.
If it is a simple URI, like google's search pages, then manually constructing it may be simpler.