【问题标题】:How to get the jpg from this url?如何从此网址获取jpg?
【发布时间】:2014-11-01 02:16:51
【问题描述】:

此 API 提供来自网站的缩略图。

<img style="-webkit-user-select: none" src="http://webthumb.bluga.net/easythumb.php?user=00000&url=www.consumerreports.com&hash=sdf9g879d8f7g9sd8fg7s9df&size=medium&cache=30">

用户 ID 和哈希值已被编辑,但如果它们是正确的,则此标记会在您的页面上生成一个名为 easythumb.jpeg 的小缩略图。

有什么方法可以获取该缩略图并将其存储在我的数据库或 AWS 中?

或者 webthumb 是否经过精心设计以防止此类行为?

编辑:

按照以下建议尝试了 Nokogiri,这是回报。看起来没有办法从中获得jpg。我说的对吗?

【问题讨论】:

  • nah - 这个问题的 url 中有一个 jpg - 我的没有。
  • 啊。看看这个:stackoverflow.com/questions/7926675/…——最佳答案建议使用 Nokogiri,我也用它来抓取/下载
  • 谢谢先生 - 现在就试试吧!
  • 您是否选择了img 标签?像这样的东西:(来自链接的答案)Nokogiri::HTML(open(URL)).xpath("//img/@src").each do |src| uri = URI.join( URL, src ).to_s # make absolute uri File.open(File.basename(uri),'wb'){ |f| f.write(open(uri).read) } end

标签: ruby-on-rails ruby nokogiri


【解决方案1】:

了解事物的作用很重要。下面是一些代码,已经测试到可以下载图片了:

require 'nokogiri'
require 'open-uri'

html = '<img style="-webkit-user-select: none" src="http://webthumb.bluga.net/easythumb.php?user=00000&url=www.consumerreports.com&hash=sdf9g879d8f7g9sd8fg7s9df&size=medium&cache=30">'
doc = Nokogiri::HTML(html)

uri = URI.parse(doc.at('img')['src']) 
# => #<URI::HTTP:0x007f8e13258520 URL:http://webthumb.bluga.net/easythumb.php?user=00000&url=www.consumerreports.com&hash=sdf9g879d8f7g9sd8fg7s9df&size=medium&cache=30>

File.basename(uri.path) 
# => "easythumb.php"

File.open(File.basename("#{ uri.path }.jpeg"), 'wb') { |fo| fo.write(open(uri).read) }

总而言之,该 URL 无效。打开浏览器页面并粘贴该 URL 会返回“Bad Hash”,而不是图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多