關於c#高級編程的第13章,有一個案例是異步獲取搜索圖片,例子使用的必應搜索api,但是需要appid和申請api,比較麻煩,所以我在想如何通過百度獲取呢。然後在百度開發人員官網找相關的api並沒有找到,所以自己就研究了下如何獲取百度圖片。這裏使用的方法是:
1. 先請求獲取搜索到的結果,然後返回搜索的原網頁的代碼。
2. 通過正則表達式和字符串截取在網頁源代碼裏得到相應的圖片信息的json數據。
3. 轉換json字符串爲xml對象。
4. 使用linq解析xml,獲取圖片url和其他信息。
5. 展示圖片到界面。
這裏貼出部分獲取圖片的代碼以供參考:
public static string GetStrHtmlByOpenRead(string url)
{
string strHTML = "";
WebClient myWebClient = new WebClient();
Stream myStream = myWebClient.OpenRead(url);
StreamReader sr = new StreamReader(myStream);
strHTML = sr.ReadToEnd();
myStream.Close();
return strHTML;
}
public static IList<XDocument> GetResponseXDocument(string strHTML)
{
//IList<string> jsonList = new List<string>();
string jsonStr = "";
IList<XDocument> XmlList = new List<XDocument>();
MatchCollection matchResult = Regex.Matches(strHTML, "[imgs:]{5}(.|\\s)+|[\"data\"]{6}(.|\\s)+");
if (matchResult.Count == 1)
{
jsonStr = matchResult[0].ToString().Replace(@" ", "").Replace(@"// 0,", "").Replace(@"//0,", "");
jsonStr = Regex.Replace(jsonStr, @"[\r\n]", "");
int i = jsonStr.IndexOf('[');
int j = jsonStr.IndexOf("}]");
jsonStr = "{\"data\":" + jsonStr.Substring(i, j - i + 2) + "}";
}
XDocument xdoc = ConvertRequestData.JsonToXDocument(jsonStr);
XmlList.Add(xdoc);
return XmlList;
}
下面是ConvertRequestData類,用來轉換json字符串:
public static class ConvertRequestData
{
public static object JsonToObject(string joson)
{
JavaScriptSerializer js = new JavaScriptSerializer();
object jsObj = (object)js.DeserializeObject(joson);
return jsObj;
}
public static IList<object> ConvertJson(IList<string> josonList)
{
JavaScriptSerializer js = new JavaScriptSerializer();
IList<object> jsList = new List<object>();
foreach (var item in josonList)
{
var josonObj = js.DeserializeObject(item);
jsList.Add(josonObj);
}
return jsList;
}
public static XmlDocument JsonToXmlDocument(string joson)
{
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
Dictionary<string, object> Dic = (Dictionary<string, object>)oSerializer.DeserializeObject(joson);
XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDec;
xmlDec = doc.CreateXmlDeclaration("1.0", "gb2312", "yes");
doc.InsertBefore(xmlDec, doc.DocumentElement);
XmlElement nRoot = doc.CreateElement("root");
doc.AppendChild(nRoot);
foreach (KeyValuePair<string, object> item in Dic)
{
XmlElement element = doc.CreateElement(item.Key);
KeyValue2Xml(element, item);
nRoot.AppendChild(element);
}
return doc;
}
public static XDocument JsonToXDocument(string json)
{
return JsonToXmlDocument(json).ToXDocument();
}
public static void KeyValue2Xml(XmlElement node, KeyValuePair<string, object> Source)
{
object kValue = Source.Value;
if (kValue.GetType() == typeof(Dictionary<string, object>))
{
foreach (KeyValuePair<string, object> item in kValue as Dictionary<string, object>)
{
XmlElement element = node.OwnerDocument.CreateElement(item.Key);
KeyValue2Xml(element, item);
node.AppendChild(element);
}
}
else if (kValue.GetType() == typeof(object[]))
{
object[] o = kValue as object[];
for (int i = 0; i < o.Length; i++)
{
XmlElement xitem = node.OwnerDocument.CreateElement("Item");
KeyValuePair<string, object> item = new KeyValuePair<string, object>("Item", o[i]);
KeyValue2Xml(xitem, item);
node.AppendChild(xitem);
}
}
else
{
XmlText text = node.OwnerDocument.CreateTextNode(kValue.ToString());
node.AppendChild(text);
}
}
public static string XmlToString(XmlDocument xml)
{
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
writer.Formatting = Formatting.Indented;
xml.Save(writer);
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
stream.Position = 0;
string XmlString = reader.ReadToEnd();
stream.Close();
reader.Close();
return XmlString;
}
}
源碼地址:http://download.csdn.net/detail/u012058778/9502938
有興趣的可以下載參考一下