方法宽度和高度机械化 [英] methods width and height Mechanize
问题描述
我使用Mechanize抓取图片网址,然后查看 http://mechanize.rubyforge.org/Mechanize/Page/Image.html 用于了解宽度和高度图像.
I'm using Mechanize for scraping images url then I'm looking http://mechanize.rubyforge.org/Mechanize/Page/Image.html for to know width and height images.
我在控制台中编写:
url = "http://www.bbc.co.uk/"
page = Mechanize.new.get(url)
images_url = page.images.map{|img| img.width}.compact
我得到结果:
["1", "84", "432", "432", "432", "432", "432", "432", "432", "304", "144", "144", "144", "144", "144", "144", "432", "432", "432", "432", "432", "432", "432", "336", "62", "62", "62", "62", "84", "1", "0"]
当我得到图像的宽度时,此结果对我来说效果很好.
This result works fine for me I get image's width.
但是,在其他网页上,我得到的却是零.您可以通过以下网页进行检查:
However with others web pages I get nil e.g. you can check with this web page:
url = "http://www.glamourum.com" #check also with https://www.birchbox.com/
page = Mechanize.new.get(url)
images_url = page.images.map{|img| img.width}.compact
我得到一个结果:
=> []
一个空数组:O或 https://www.birchbox.com/我得到一个数组与:
an array empty :O or for https://www.birchbox.com/ I get an array with:
=> ["1", "1", "1", "1", "1"]
为什么在某些网站上会发生这种情况,而在其他网站上却不会发生?
该问题的解决方案是什么?
推荐答案
机械化不会获取图像.它只能返回HTML上img
标记所反映的大小,而很多网站都没有包含该大小.
Mechanize doesn't fetch the images. It can only return you the size as reflected on the img
tag in the HTML and a lot sites don't include that.
这篇关于方法宽度和高度机械化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!