首页 > 技术文章 > c# 解析百度图片搜索结果json中objURL图片原始地址

wgscd 2018-03-23 15:22 原文

// http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord=mm&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word=mm&face=0&istype=2&nc=1&pn=120&rn=60&gsm=3c&1521787928351=&callback=angular.callbacks._0

 

 

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace WpfApplication1
{
    public class Utils
    {
        public static string baidtu_uncomplie(string str)
        {   //ippr_z2C$qAzdH3FAzdH3Ft429_z&e3B17tpwg2_z&e3Bv54AzdH3F7rs5w1fAzdH3Ftpj4AzdH3Fda89acAzdH3FdnAzdH3Fda89acdn8mc099_GWcwo_z&e3B3rj2
            string[] c = { "_z2C$q", "_z&e3B", "AzdH3F" };
            Dictionary<String, String> d = new Dictionary<string, string>();
            d.Add("w", "a"); d.Add("k", "b"); d.Add("v", "c"); d.Add("1", "d"); d.Add("j", "e"); d.Add("u", "f"); d.Add("2", "g");
            d.Add("i", "h"); d.Add("t", "i"); d.Add("3", "j"); d.Add("h", "k"); d.Add("s", "l"); d.Add("4", "m"); d.Add("g", "n");
            d.Add("5", "o"); d.Add("r", "p"); d.Add("q", "q"); d.Add("6", "r"); d.Add("f", "s"); d.Add("p", "t"); d.Add("7", "u");
            d.Add("e", "v"); d.Add("o", "w"); d.Add("8", "1"); d.Add("d", "2"); d.Add("n", "3"); d.Add("9", "4"); d.Add("c", "5");
            d.Add("m", "6"); d.Add("0", "7"); d.Add("b", "8"); d.Add("l", "9"); d.Add("a", "0"); d.Add("_z2C$q", ":"); d.Add("_z&e3B", "."); d.Add("AzdH3F", "/");
            if (!(str != null) || str.Contains("http"))
                return str;
            string j = str;
            foreach (string s in c)
            {
                j = j.Replace(s, d[s]);
            }
            string[] arr = SplitByLen(j, 1);
            for (int i = 0; i < arr.Length; i++)
            {
                try
                {
                    if (Regex.IsMatch(d[arr[i]], @"^[a-w\d]+$"))
                    {
                        arr[i] = d[arr[i]];
                    }
                }
                catch     //不匹配的不做处理:   ".   :   /"
                {
                }
            }
            string url = string.Join("", arr);
            return url;
        }
        //字符串按长度截图
        private static string[] SplitByLen(string str, int separatorCharNum)
        {
            if (string.IsNullOrEmpty(str) || str.Length <= separatorCharNum)
            {
                return new string[] { str };
            }
            string tempStr = str;
            List<string> strList = new List<string>();
            int iMax = Convert.ToInt32(Math.Ceiling(str.Length / (separatorCharNum * 1.0)));//获取循环次数    
            for (int i = 1; i <= iMax; i++)
            {
                string currMsg = tempStr.Substring(0, tempStr.Length > separatorCharNum ? separatorCharNum : tempStr.Length);
                strList.Add(currMsg);
                if (tempStr.Length > separatorCharNum)
                {
                    tempStr = tempStr.Substring(separatorCharNum, tempStr.Length - separatorCharNum);
                }
            }
            return strList.ToArray();
        }
        //调用:Utils.baidtu_uncomplie("ippr_z2C$qAzdH3FAzdH3Ft429_z&e3B17tpwg2_z&e3Bv54AzdH3F7rs5w1fAzdH3Ftpj4AzdH3Fda89acAzdH3FdnAzdH3Fda89acdn8mc099_GWcwo_z&e3B3rj2")
    }
}

推荐阅读