嘗試https://en.wikipedia.org/使用 mechanize gem從網站上抓取影像。Mechanize::ResponseCodeError (404 => Net::HTTPNotFound for https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/FP2A3620_%2823497688248%29.jpg/119px-FP2A3620_%2823497688248%29.jpg -- unhandled response):當我嘗試計算影像大小時,我得到了這個。
這是我的代碼
def images
agent = Mechanize.new
page = agent.get("https://en.wikipedia.org/")
page.images.each do |image|
puts image.url
size = agent.head( image )["content-length"].to_i/1000
end
end
任何幫助表示贊賞。
uj5u.com熱心網友回復:
在維基百科上照看那個影像,它呈現得很好。在新選項卡中打開它,并將瀏覽器中的 url 與 mechanize 的內容進行比較。
取消轉義網址,成功了。
image_url = CGI.unescape(image.url.to_s)
size = agent.head(image_url)["content-length"].to_i/1000
這是一個有效的Replit。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/365327.html
