ruby - Nokogiri 抓取网页乱码如何解决?
巴扎黑
巴扎黑 2017-04-22 09:00:21
0
1
717

最近在帮同学在一个网站上抓取一些东西,比如 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 等等,都无效。。

这种情况该如何解决?

巴扎黑
巴扎黑

répondre à tous(1)
阿神
doc = Nokogiri::HTML(open('http://www.sell001.com/110178'),nil,'UTF-8')

=> #(Document :0x3fc3974355f4 {
nom = "document",
enfants = [
#(DTD:0x3fc397424bf0 { nom = "html" }),
#(Élément :0x3fc39741fc18 {
nom = "html",
attributs = [
#(Attr:0x3fc39740fa20 {
nom = "xmlns",
valeur = "http://www.w3.org/1999/xhtml"
})],
enfants = [
#(Texte "rn"),
#(Élément :0x3fc3973da190 {
nom = "tête",
enfants = [
#(Texte "rn"),
#(Élément :0x3fc3973cf6dc {
nom = "titre",
enfants = [ #(Texte "九天国际A218-网商园")]
}),

utf-8正常解析。。。

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!