zoukankan      html  css  js  c++  java
  • HtmlAgilityPack 抓取页面的乱码处理

    HtmlAgilityPack 抓取页面的乱码处理


    用来解析 HTML 确实方便。不过直接读取网页时会出现乱码。
    实际上,它是能正确读到有关字符集的信息,怎么会在输出时,没有取到正确内容。
    因此,读两次就可以了。

    #if interactive
    #r @"E:htmlagilitypack-99964TrunkHtmlAgilityPackinfx.4.0DebugHtmlAgilityPack.dll"
    #endif
    //open System
    //open System.Xml

    //   hexun -- GB2312
    //   tianya -- UTF-8

    //let url = @"http://hadstj.blog.hexun.com/88419406_d.html"
    let url = @"http://bbs.tianya.cn/post-worldlook-873912-1.shtml"
    let html = new HtmlAgilityPack.HtmlWeb()
    let doc0 = html.Load  ( url )
    html.OverrideEncoding <- doc0.Encoding
    let doc = html.Load  ( url )

    let title = doc.DocumentNode.SelectSingleNode("//title").InnerText
    printfn "%s" title

    doc.DocumentNode.SelectNodes("//div")
    |> Seq.map ( fun i -> i.InnerText )
    |> Seq.iter   ( printfn "%s" )

  • 相关阅读:
    [iOS基础控件
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    linux开发脚本自动部署及监控
  • 原文地址:https://www.cnblogs.com/pangblog/p/3320346.html
Copyright © 2011-2022 走看看