开发者

Pick out stuff from site?

Is there a way, using C#.Net, to basically use something like http://www.bing.com/images/search?q=microsoft&form=QBIL&qs=n&sk=&sc=8-4, extract all the ima开发者_如何学编程ges from it, and put it into a file?


You can use the HTML Agility Pack and its HTMLWeb class to parse a web page.


If you would like to do it a bit cleaner then go for bing API its the best way to do it. In its JSON/XML/SOAP response you will get url to each Image in the result, You can retrive those images in a Loop or more better in a LINQ query.

Check out this PDF which tell you the basic for get going.

Here is an example of how you can do it. First get an APPID that allow you to make API Queries then.

make a request like this

string url = "http://api.search.live.net/xml.aspx?Appid={0}&sources={1}&query={2}";
string completeUri = String.Format(url, AppId, "image", "microsoft");
HttpWebRequest webRequest = null;
webRequest = (HttpWebRequest)WebRequest.Create(completeUri);
HttpWebResponse webResponse = null;
webResponse = (HttpWebResponse)webRequest.GetResponse();
XmlReader xmlReader = null;
xmlReader = XmlReader.Create(webResponse.GetResponseStream());

then create a class that will keep the returned data.

public class LiveSearchResultImage
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string URI { get; set; }
    public string ImageURI { get; set; }
    public string ThumbnailURI { get; set; }
}

and then retrive the data from response using a LINQ query.

XDocument data = XDocument.Load(xmlReader);
IEnumerable<XNode> nodes = null;
nodes = data.Descendants(XName.Get("Results", IMAGE_NS)).Nodes();
if (nodes.Count() > 0)
{
    var results = from uris in nodes
    select new LiveSearchResultImage
    {
    URI =
    ((XElement)uris).Element(XName.Get("Url", IMAGE_NS)).Value,
    Title =
    ((XElement)uris).Element(XName.Get("Title", IMAGE_NS)).Value,
    ThumbnailURI =
    ((XElement)uris).Element(XName.Get("Thumbnail", IMAGE_NS)).Value,
    };
    return results;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜