简体   繁体   English

如何在Ruby上使用Nokogiri在终端上查看HTML?

[英]How to view HTML on terminal using Nokogiri on Ruby?

I'm trying to parse the HTML page of wikipedia using Nokogiri on Ruby(2.5.1) on Ubuntu, here is my code and what my terminal says back: 我正在尝试使用Ubuntu上的Ruby(2.5.1)上的Nokogiri解析Wikipedia的HTML页面,这是我的代码以及终端机回复的内容:

Any ideas on where the problem comes from ? 关于问题来自何处的任何想法? I tried bundle install just before but nothing seems to work. 我在之前尝试过捆绑安装,但似乎没有任何效果。 Thanks in advance for any help brought ! 在此先感谢您提供的任何帮助!

require 'open-uri'
require 'nokogiri'

page = Nokogiri::HTML(open('https://en.wikipedia.org'))   
puts page   # => Nokogiri::HTML::Document```



```asus@asus-X75VD:~/THP/jour8/lib$ ruby test8.rb 
Nokogiri::HTML::Document```


Just so you know, Nokogiri has a command-line equivalent that lets you retrieve a page and play with it in IRB so you don't have to mess with writing code until you're ready to. 众所周知,Nokogiri具有等效的命令行功能,使您可以检索页面并在IRB中进行操作,因此在您准备好编写代码之前就不必费心。 If you enter: 如果输入:

nokogiri https://en.wikipedia.org

at the terminal prompt you'll drop into IRB and be able to do something like: 在终端提示下,您将进入IRB并能够执行以下操作:

irb(main):002:0> @doc.to_s[0..10]
=> "<!DOCTYPE h"

or: 要么:

irb(main):005:0> @doc.to_s.size
=> 76139

You can view the page, write it to disk, all the normal things. 您可以查看页面,将其写入磁盘,所有正常的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM