使用htmlagilitypack获取Google图片 [英] Fetching google images using htmlagilitypack

查看:66
本文介绍了使用htmlagilitypack获取Google图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Google图片上执行查询,以使用C#中的htmlagilitypack获取图片. 为此,我对图像使用了xpath请求

I would like to execute a query on google images to fetch images using htmlagilitypack in c#. For this I used an xpath request to the image

//*[@id="rg_s"]/div[1]/a/img

但是它无法以这种方式获取图像.这样做的正确方法是什么?

But it fails to fetch the image that way. What could be the correct way of doing this?

推荐答案

以编程方式在Google API之外搜索Google违反了 Google自定义搜索

Searching google programmatically outside of their API's is against the TOS. Consider Google Custom Search or Bing Search API, both of which have established JSON and SOAP interfaces.

这两种服务每月都是免费的,并且符合服务的服务条款.

Both are free for a couple thousand queries per month and comply with the service's TOS.

下面的在C#中使用Bing API的示例:

Examples of using Bing API with C# below:

const string bingKey = "[your key here]";
var bing = new BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search/")) 
{ 
    Credentials = new NetworkCredential(bingKey, bingKey) 
};

var query = bing.Web("Jon Gallant blog", null, null, null, null, null, null, null);
var results = query.Execute();

foreach(var result in results)
{
    Console.WriteLine(result.Url);
}
Console.ReadKey();

Google自定义搜索API:

Google custom search API:

string apiKey = "Your api key";
string cx = "Your custom search engine id";
string query = "Your query";

var svc = new Google.Apis.Customsearch.v1.CustomsearchService(new BaseClientService.Initializer { ApiKey = apiKey });
var listRequest = svc.Cse.List(query);

listRequest.Cx = cx;
var search = listRequest.Fetch();

foreach (var result in search.Items)
{
    Response.Output.WriteLine("Title: {0}", result.Title);
    Response.Output.WriteLine("Link: {0}", result.Link);
}

这篇关于使用htmlagilitypack获取Google图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆