【问题标题】:How can I parse HTML Document to look for og:image tag using regular expressions?如何使用正则表达式解析 HTML 文档以查找 og:image 标记?
【发布时间】:2013-07-29 08:41:39
【问题描述】:

我以编程方式下载了网页内容并将其保存在字符串变量中。查找“og:image”元标记内容网址的最佳方法是什么?

例如假设来自页面视图源的 sn-p 如下所示:

<meta property="og:site_name" content="The Christian Science Monitor"  />
<meta property="og:type" content="article"  />
<meta property="og:url" content="http://www.csmonitor.com/Business/2013/0729/Cannes-jewel-heist-53-million-in-diamonds-jewels-stolen-from-hotel"  />
<meta property="og:description" content="Cannes jewel heist saw $53 million in diamonds and other precious gems stolen from a hotel on the French Riviera. The Cannes jewel heist is the latest in a series of several brazen jewelry thefts in Europe in recent years."  />
<meta property="og:image" content="http://www.csmonitor.com/var/ezflow_site/storage/images/media/content/2013/0729-jewels/16474969-1-eng-US/0729-jewels.jpg"  />
<meta property="og:title" content="Cannes jewel heist: $53 million in diamonds, jewels stolen from hotel"  />
<meta name="sailthru.author" content="Thomas Adamson"  />

我想提取作为“og:image”标签目标的“http://www.csmonitor.com/var/ezflow_site/storage/images/media/content/2013/0729-jewels/16474969-1-eng-US/0729-jewels.jpg”字符串。

我可以在代码中构造一些逻辑来查找子字符串,然后从那里获取它,但我想使用类似于此的正则表达式语法来完成此操作:

List<Uri> links = new List<Uri>();
string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";

MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);

最后一个示例抓取网页源并提取所有图像标签。我想对 og:image 标签做同样的事情,但我不太精通正则表达式。

【问题讨论】:

  • 不要使用正则表达式解析 HTML。使用适当的 HTML 解析模块。 您无法使用正则表达式可靠地解析 HTML,并且您将面临悲伤和挫败感。一旦 HTML 与您的期望发生变化,您的代码就会被破坏。
  • 谢谢安迪。你能推荐一个合适的 HTML 解析模块吗?我在 C# 环境中。
  • 对不起,我不能。我根本不了解 C# 世界。在 StackOverflow 或 Google 中搜索“html parsing C#”,看看你能找到什么。

标签: c# regex html-parsing


【解决方案1】:

我认为你不应该使用正则表达式,它可能会有点古怪,这取决于他们如何将其放入 html 中。例如,content= 可能在 property= 之前。 我确实使用了一些常规代码,我不想使用 html 或 xml 解析器插件。这就是我最终要做的事情。

Dictionary<string, string> metatags = new Dictionary<string, string>();
int TagStart,TagEnd;
string element;
int AttrStart, AttrEnd;
string PropVal,ContentVal;
TagStart = strIn.IndexOf("<meta", StringComparison.OrdinalIgnoreCase);
while(TagStart != -1) {
    TagEnd = strIn.IndexOf(">", TagStart + 1, StringComparison.OrdinalIgnoreCase);
    if (TagEnd != -1) {
        element = strIn.Substring(TagStart, TagEnd - TagStart + 1);
        //Console.WriteLine("\nPROCESSING META TAG: {0}",element);
        PropVal = null;
        ContentVal = null;

        // Get "property" attribute
        AttrStart = element.IndexOf("property=\"", StringComparison.OrdinalIgnoreCase);
        if (AttrStart != -1) {
            AttrStart = AttrStart + 10;
            AttrEnd = element.IndexOf("\"", AttrStart, StringComparison.OrdinalIgnoreCase);
            if(AttrEnd != -1) {
                PropVal = element.Substring(AttrStart, AttrEnd - AttrStart);
            }
        }
        // Get "content" attribute
        AttrStart = element.IndexOf("content=\"", StringComparison.OrdinalIgnoreCase);
        if(AttrStart != -1) {
            AttrStart = AttrStart + 9;
            AttrEnd = element.IndexOf("\"", AttrStart, StringComparison.OrdinalIgnoreCase);
            if(AttrEnd != -1) {
                ContentVal = element.Substring(AttrStart, AttrEnd - AttrStart);
            }
        }
        if (PropVal != null && ContentVal != null)
            metatags.Add(PropVal, ContentVal);

    }
    // go to next meta tag
    TagStart = strIn.IndexOf("<meta", TagStart + 1, StringComparison.OrdinalIgnoreCase);
}
Console.WriteLine("\nOG meta tags");
foreach(var item in metatags) {
    Console.WriteLine("KEY={0} VALUE={1}",item.Key,item.Value);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 2011-04-22
    • 2013-07-23
    • 2016-09-23
    • 2023-04-02
    • 2015-06-30
    • 1970-01-01
    • 2014-06-08
    相关资源
    最近更新 更多