读淘宝页面字节流提取宝贝图片地址宝贝标题宝贝价格
生活随笔
收集整理的這篇文章主要介紹了
读淘宝页面字节流提取宝贝图片地址宝贝标题宝贝价格
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public static class taobao_message
??????? {
??????????? /// <summary>
??????????? /// 讀頁面的byte轉化為string
??????????? /// </summary>
??????????? /// <param name="url">地址</param>
??????????? /// <returns></returns>
??????????? public static string webclinet_content(string url)
??????????? {
??????????????? url = url.Replace("http://", "");
??????????????? System.Net.WebClient client = new WebClient();
??????????????? byte[] page = client.DownloadData("http://" + url); ?
??????????????? string content = System.Text.Encoding.GetEncoding("GB2312").GetString(page);//淘寶的頁面編碼為gb2312
??????????????? return content;
??????????? }
??????????? /// <summary>
??????????? /// 讀淘寶寶貝的信息
??????????? /// 數組內容{寶貝圖片地址,寶貝標題,寶貝價格}
??????????? /// </summary>
??????????? /// <param name="url">寶貝地址</param>
??????????? /// <returns></returns>
??????????? public static string[] baobei_mess(string url)
??????????? {
??????????????? string content = webclinet_content(url);
??????????????? string baobei_img =get_taobao(content,1);
??????????????? string baobei_title = get_taobao(content,2);
??????????????? string baobei_price = get_taobao(content,3);
??????????????? string[] arry1 = {baobei_img,baobei_title,baobei_price};
??????????????? return arry1;;
??????????? }
??????????? /// <summary>
??????????? /// 特定標簽內容提取
??????????? /// </summary>
??????????? /// <param name="content">提取的字符串</param>
??????????? /// <param name="type">驗證類型0:空; 1:淘寶貝圖片;2:寶貝標題;3:寶貝價格;</param>
??????????? /// <returns></returns>
??????????? public static string get_taobao(string content, int type)
??????????? {
??????????????? string result = "";
??????????????? string reg = "";
??????????????? switch (type)
??????????????? {
??????????????????? case 0: return "";
??????????????????? case 1: reg = @"J_ImgBooth\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>"; break;
??????????????????? case 2: reg = "<h3>(<a[^>]*>)?([^<]*)(</a>)?</h3>"; break;
??????????????????? case 3: reg = "J_StrPrice[^>]*>([^<>]*)(</)"; break;
??????????????? }
??????????????? string regex = reg;
??????????????? Regex re = new Regex(regex);
??????????????? MatchCollection matches = re.Matches(content);
??????????????? System.Collections.IEnumerator enu = matches.GetEnumerator();
??????????????? switch (type)
??????????????? {
??????????????????? case 0: return "";
??????????????????? case 1:
??????????????????????? while (enu.MoveNext() && enu.Current != null)
??????????????????????? {
??????????????????????????? Match match = (Match)(enu.Current);
??????????????????????????? result += match.Groups["imgUrl"];
??????????????????????? } break;
??????????????????? case 2:
??????????????????????? while (enu.MoveNext() && enu.Current != null)
??????????????????????? {
??????????????????????????? Match match = (Match)(enu.Current);
??????????????????????????? result += match.Groups[2];
??????????????????????? } break;
??????????????????? case 3:
??????????????????????? while (enu.MoveNext() && enu.Current != null)
??????????????????????? {
??????????????????????????? Match match = (Match)(enu.Current);
??????????????????????????? result += match.Groups[1];
??????????????????????? } break;
??????????????? }
??????????????? return result;
?????????? }
??????? }
??????? {
??????????? /// <summary>
??????????? /// 讀頁面的byte轉化為string
??????????? /// </summary>
??????????? /// <param name="url">地址</param>
??????????? /// <returns></returns>
??????????? public static string webclinet_content(string url)
??????????? {
??????????????? url = url.Replace("http://", "");
??????????????? System.Net.WebClient client = new WebClient();
??????????????? byte[] page = client.DownloadData("http://" + url); ?
??????????????? string content = System.Text.Encoding.GetEncoding("GB2312").GetString(page);//淘寶的頁面編碼為gb2312
??????????????? return content;
??????????? }
??????????? /// <summary>
??????????? /// 讀淘寶寶貝的信息
??????????? /// 數組內容{寶貝圖片地址,寶貝標題,寶貝價格}
??????????? /// </summary>
??????????? /// <param name="url">寶貝地址</param>
??????????? /// <returns></returns>
??????????? public static string[] baobei_mess(string url)
??????????? {
??????????????? string content = webclinet_content(url);
??????????????? string baobei_img =get_taobao(content,1);
??????????????? string baobei_title = get_taobao(content,2);
??????????????? string baobei_price = get_taobao(content,3);
??????????????? string[] arry1 = {baobei_img,baobei_title,baobei_price};
??????????????? return arry1;;
??????????? }
??????????? /// <summary>
??????????? /// 特定標簽內容提取
??????????? /// </summary>
??????????? /// <param name="content">提取的字符串</param>
??????????? /// <param name="type">驗證類型0:空; 1:淘寶貝圖片;2:寶貝標題;3:寶貝價格;</param>
??????????? /// <returns></returns>
??????????? public static string get_taobao(string content, int type)
??????????? {
??????????????? string result = "";
??????????????? string reg = "";
??????????????? switch (type)
??????????????? {
??????????????????? case 0: return "";
??????????????????? case 1: reg = @"J_ImgBooth\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>"; break;
??????????????????? case 2: reg = "<h3>(<a[^>]*>)?([^<]*)(</a>)?</h3>"; break;
??????????????????? case 3: reg = "J_StrPrice[^>]*>([^<>]*)(</)"; break;
??????????????? }
??????????????? string regex = reg;
??????????????? Regex re = new Regex(regex);
??????????????? MatchCollection matches = re.Matches(content);
??????????????? System.Collections.IEnumerator enu = matches.GetEnumerator();
??????????????? switch (type)
??????????????? {
??????????????????? case 0: return "";
??????????????????? case 1:
??????????????????????? while (enu.MoveNext() && enu.Current != null)
??????????????????????? {
??????????????????????????? Match match = (Match)(enu.Current);
??????????????????????????? result += match.Groups["imgUrl"];
??????????????????????? } break;
??????????????????? case 2:
??????????????????????? while (enu.MoveNext() && enu.Current != null)
??????????????????????? {
??????????????????????????? Match match = (Match)(enu.Current);
??????????????????????????? result += match.Groups[2];
??????????????????????? } break;
??????????????????? case 3:
??????????????????????? while (enu.MoveNext() && enu.Current != null)
??????????????????????? {
??????????????????????????? Match match = (Match)(enu.Current);
??????????????????????????? result += match.Groups[1];
??????????????????????? } break;
??????????????? }
??????????????? return result;
?????????? }
??????? }
總結
以上是生活随笔為你收集整理的读淘宝页面字节流提取宝贝图片地址宝贝标题宝贝价格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: google chrome 浏览器 必
- 下一篇: 云计算的虚拟交换机