Google图片搜索Xpath - 提取部分Text()

时间:2016-05-13 15:14:22

标签: c# .net xpath web-crawler

我需要很多图片。一个很好的来源当然是Google Image搜索。

我一直在寻找最好的方法来做到这一点。获得更小的"缩略图"图像是可能的,但我希望有原始尺寸。

使用:

 //*[@id="rg_s"]/div/div/text()

我找到了原始大小的网址。例如:

{"cb":9,"cl":9,"cr":9,"ct":9,"id":"twpCKa-qACVbrM:","isu":"twitter.com",
"itg":false,"ity":"jpg","oh":512,"ou":
"https://pbs.twimg.com/profile_images/698459967624474624/FsezpZpl.jpg",
"ow":512,"pt":"Manchester United (@ManUtd) | Twitter","rid":"5Q1F7uGUbUotPM",
"ru":"https://twitter.com/manutd","s":"","sc":1,"th":225,"tu":
"https://encrypted-tbn2.gstatic.com/images? 
q\u003dtbn:ANd9GcRELkTX0VqGU4OHs9sgS93dedTCNsW0TvJT3S72YuOCCHfXxZSa","tw":225}

使用: 的 https://pbs.twimg.com/profile_images/698459967624474624/FsezpZpl.jpg

成为原始尺寸的网址。我真的不知道这个文本块在网站上的实际位置。但我想知道的是,它本身的URL是否可以被隔离和提取?

1 个答案:

答案 0 :(得分:1)

您无法使用XPath提取JSON值的部分内容,但您可以对使用XPath找到的文本值使用正则表达式。例如:

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {

            //Load XML ....
            //string s = xml.SelectSingleNode('//*[@id="rg_s"]/div/div/text()').Value
            string s = @"{""cb"":9,""cl"":9,""cr"":9,""ct"":9,""id"":""twpCKa-qACVbrM:"",""isu"":""twitter.com"",
""itg"":false,""ity"":""jpg"",""oh"":512,""ou"":
""https://pbs.twimg.com/profile_images/698459967624474624/FsezpZpl.jpg"",
""ow"":512,""pt"":""Manchester United (@ManUtd) | Twitter"",""rid"":""5Q1F7uGUbUotPM"",
""ru"":""https://twitter.com/manutd"",""s"":"""",""sc"":1,""th"":225,""tu"":
""https://encrypted-tbn2.gstatic.com/images? 
q\u003dtbn:ANd9GcRELkTX0VqGU4OHs9sgS93dedTCNsW0TvJT3S72YuOCCHfXxZSa"",""tw"":225}";

            Console.WriteLine(System.Text.RegularExpressions.Regex.Match(s, "\"ou\":\\s*?\"([^\"]+)\"").Groups[1].Value);
            Console.ReadKey();

        }
    }
}