爬取知乎百萬資訊之UrlTask

來源:互聯網
上載者:User

標籤:private   伺服器   序列化   返回   解析   aging   資訊   應該   isnull   

這個模組的作用是從nexturl隊列擷取使用者的關注列表的url,擷取關注列表。伺服器返回的Json的資料

封裝一個對象的序列化和還原序列化的類

public   class SerializeHelper    {        /// <summary>        /// 對資料進行序列化        /// </summary>        /// <param name="value"></param>        /// <returns></returns>        public static string SerializeToString(object value)        {            return JsonConvert.SerializeObject(value);        }        /// <summary>        /// 還原序列化操作        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="str"></param>        /// <returns></returns>        public static T DeserializeToObject<T>(string str)        {                      return JsonConvert.DeserializeObject<T>(str);        }}

封裝UrlTask類

 public class UrlTask    {        private  string url { get; set; }        private string JSONstring { get; set; }        public UrlTask(string _url)        {            url = _url;          }}

添加一個擷取資源的方法

 private bool GetHtml()        {            JSONstring= HttpHelp.DownLoadString(url);            Console.WriteLine("Json下載完成");            return !string.IsNullOrEmpty(JSONstring);        }
解析json方法
 public  void  Analyse()         {            try            {                if (GetHtml())                {                    Stopwatch watch = new Stopwatch();                    watch.Start();                                   followerResult result = SerializeHelper.DeserializeToObject<followerResult>(JSONstring);                     if (!result.paging.is_end)                     {                         RedisCore.PushIntoList(1, "nexturl", result.paging.next);                                      }                                               foreach (var item in result.data)                    {                         int type=Math.Abs(item.GetHashCode())% 3 + 3;                         if (RedisCore.InsetIntoHash(type, "urltokenhash", item.url_token, "存在"))                         {                             RedisCore.PushIntoList(1, "urltoken", item.url_token);                                                    }                                          }                    watch.Stop();                    Console.WriteLine("解析json用了{0}毫秒",watch.ElapsedMilliseconds.ToString());                }            }            catch (Exception ex)            {                Console.WriteLine(ex.ToString());            }                  }

解析:如果result.paging.is_end為true,那麼這個是使用者關注列表的最後一頁,那麼它的nexturl應該排入佇列,負責不要加入,對於後面的使用者數組,因為資訊不去全,不要了,有了Id前往首頁擷取詳細資料。


 

 

 

 

爬取知乎百萬資訊之UrlTask

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.