关于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
有兴趣的可以下载参考一下