如何使用Nokogiri从特定URL上爬取图片?如果有比Nokogiri更好的选择,请建议。CSS图像标签为.profilePic img
。
如何使用Nokogiri从特定URL上爬取图片?如果有比Nokogiri更好的选择,请建议。CSS图像标签为.profilePic img
。
<img>
标签:PAGE = "http://site.com/page.html"
require 'nokogiri'
require 'open-uri'
html = Nokogiri.HTML(open(PAGE))
src = html.at('.profilePic img')['src']
File.open("foo.png", "wb") do |f|
f.write(open(src).read)
end
如果您需要将相对路径转换为绝对路径,请参见:
https://dev59.com/mm445IYBdhLWcg3wcJ2O#4864170
require 'mechanize'
agent = Mechanize.new
doc = agent.get(url)
agent.get(doc.parser.at('.profilePic img')['src']).save