首页 > Nokogiri 抓取网页乱码如何解决?

Nokogiri 抓取网页乱码如何解决?

最近在帮同学在一个网站上抓取一些东西,比如 http://www.sell001.com/110178 上面的服装图片,标题和价格等等。

这个网站有点奇怪,他 meta 里面没有声明 charset,然后我用 Nokogiri ,也没有指定特定的 encoding。

抓了一些图片和链接都是可以的,但是碰到有中文的情况时它就乱码了。

我看官方文档 http://www.nokogiri.org/tutorials/parsing_an_html_xml_document.html,
Nokogiri 可以指定 encoding,比如 doc = Nokogiri.XML('<foo><bar /><foo>', nil, 'EUC-JP')
我试了指定一些比如 gbk 等等,都无效。。

这种情况该如何解决?


doc = Nokogiri::HTML(open('http://www.sell001.com/110178'),nil,'UTF-8')

=> #(Document:0x3fc3974355f4 {
name = "document",
children = [
#(DTD:0x3fc397424bf0 { name = "html" }),
#(Element:0x3fc39741fc18 {
name = "html",
attributes = [
#(Attr:0x3fc39740fa20 {
name = "xmlns",
value = "http://www.w3.org/1999/xhtml"
})],
children = [
#(Text "\r\n"),
#(Element:0x3fc3973da190 {
name = "head",
children = [
#(Text "\r\n"),
#(Element:0x3fc3973cf6dc {
name = "title",
children = [ #(Text "九天国际A218-网商园")]
}),

utf-8正常解析。。。

【热门文章】
【热门文章】