HtmlAgilityPack的dll调用文件获取数据的乱码问题

tech2023-10-18  102

使用HtmlAgilityPack爬取网页数据乱码问题

在HtmlAgilityPack项目中修改HtmlWeb.cs文件下的内容,重新生成新的dll调用文件可解决乱码问题。

//******************注释以前代码部分***************************** //Encoding respenc = !string.IsNullOrEmpty(resp.ContentEncoding) // ? Encoding.GetEncoding(resp.ContentEncoding) // : null; //Encoding respenc = !string.IsNullOrEmpty(resp.ContentEncoding) // ? Encoding.GetEncoding(resp.ContentEncoding) // : null; //********************新加代码部分*********************************** System.Text.Encoding respenc; if ((resp.ContentEncoding != null) && (resp.ContentEncoding.Length > 0)) { respenc = System.Text.Encoding.GetEncoding(resp.ContentEncoding); } else if ((resp.CharacterSet != null) && (resp.CharacterSet.Length > 0))//根据Content-Type中获取的charset { if (string.Compare(resp.CharacterSet, "ISO-8859-1", true, System.Globalization.CultureInfo.InvariantCulture) == 0) respenc = System.Text.Encoding.GetEncoding("GB2312"); else respenc = System.Text.Encoding.GetEncoding(resp.CharacterSet); } else { respenc = System.Text.Encoding.GetEncoding("GB2312"); } //******************8**********END*********************************

附修改后的Dll项目打包文件 下载地址 https://download.csdn.net/download/weixin_42404384/12805269

参考文章https://blog.csdn.net/W3031213101/article/details/5801888?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159911816419195162123329%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=159911816419195162123329&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v3~rank_business_v1-1-5801888.ecpm_v3_rank_business_v1&utm_term=htmlagilitypack%E4%B8%AD%E6%95%B0%E6%8D%AE%E4%B9%B1%E7%A0%81&spm=1018.2118.3001.4187

最新回复(0)