zoukankan      html  css  js  c++  java
  • .net core不支持gb2312编码的问题

    在用net core写爬虫的时候,发现默认不再支持gb2312编码了:

    解决方案如下:

    1,引入System.Text.Encoding.CodePages:

    2,在需要的地方注册EncodingProvider的方法;

    3,调用 Encoding.GetEncoding("GB2312").GetString(pageSource);

    public void CityCrawler(string allCityUrl)
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                var cityList = new List<string>();
                var pageSource = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip })
                        .GetByteArrayAsync(allCityUrl).Result;
                var result = Encoding.GetEncoding("GB2312").GetString(pageSource);
    
                var cities = Regex.Matches(result,
                    "([u4e00-u9fa5]{2,5})", "spell": "[A-Za-z]+", "url": "//([A-Za-z]{2,}.esf.fang.com)");
                for (int index = 0; index < cities.Count; index++)
                {
                    var city = cities[index].Groups[1].Value + ":" + "https://" + cities[index].Groups[2].Value;
                    cityList.Add(city);
                }
                File.WriteAllLines("房天下城市列表.txt", cityList);
            }
  • 相关阅读:
    指针和数组的关系
    深入学习数组
    const关键字与指针
    野指针是什么
    指针带来的一些符号的理解
    指针的本质
    内存管理之堆
    内存管理之栈
    元类
    断点调式和面向对象进阶
  • 原文地址:https://www.cnblogs.com/Zdelta/p/14122328.html
Copyright © 2011-2022 走看看