srb*_*ert 0 ruby nokogiri web-scraping
我有一个脚本适用于我想要抓取的99%的页面,但只有少数几个没有我想要的东西而且我的脚本错误了
undefined method `attribute' for nil:NilClass (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
从摆弄和调试代码有点难看,但这就是我正在做的事情.错误在第三行,只是因为在错误情况下没有.entry-content img:
doc = Nokogiri::HTML(open(url))
image_link = doc.css(".entry-content img")
temp = image_link.attribute('src').to_s
Run Code Online (Sandbox Code Playgroud)
当Nokogiri返回的image_link不是nil时,如何检测到这一点并处理错误?
doc = Nokogiri::HTML(open(url))
if image_link = doc.at_css(".entry-content img")
temp = image_link['src']
else
# Whatever else
end
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用XPath选择器直接获取属性值:
doc = Nokogiri::HTML('<div class="entry-content"><img src="bar"></div>')
src = doc.at_xpath('//*[@class="entry-content"]//img/@src').to_s
# src is "bar"; if the html didn't have such an item, it would be "" (nil.to_s)
Run Code Online (Sandbox Code Playgroud)