【问题标题】:Google images search Xpath - extract part of Text()谷歌图像搜索 Xpath - 提取部分 Text()
【发布时间】:2016-05-13 15:14:22
【问题描述】:

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

我一直在寻找最好的方法来做到这一点。获得较小的“缩略图”图像是可能的,但我想要原始尺寸。

使用:

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

我确实找到了原始大小的 URL。例如:

{"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。我真的不知道这个文本块实际上可以在网站上的哪个位置找到。但是我想知道的是,它自己的 URL 是否可以被隔离和提取?

【问题讨论】:

  • 不在 XPath 中。您需要用其他东西解析内部 JSON。

标签: c# .net xpath web-crawler


【解决方案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();

        }
    }
}

【讨论】:

  • 对不起,我没有及时回复,非常感谢您的帮助。我之前没有使用过很多正则表达式,所以如果可以进一步解释它会很有帮助。我猜 "\"ou\":\\s*?\"([^\"]+)\"").Groups[1].Value 意味着它找到了 "ou" 并以某种方式获取了链接。跨度>
  • 当它找到 ou:"somevalue" 时,它会匹配 somevalue 并将其存储在捕获组 ( .. ) 中,然后您访问该组并获取值。演示和解释见 Regex101:regex101.com/r/cQ3uZ8/1
猜你喜欢
  • 2011-05-21
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 2019-09-20
  • 2012-08-19
  • 1970-01-01
相关资源
最近更新 更多