如何从URL获取网页标题和图像?

时间:2011-03-23 04:53:34

标签: c# jquery asp.net jquery-plugins

我想从网址获取网站标题和图片。
当facebook.com在做。我如何从第三方链接获取图像和网站标题。?

4 个答案:

答案 0 :(得分:5)

使用html Agility Pack这是获取标题的示例代码:

using System;
using HtmlAgilityPack;

protected void Page_Load(object sender, EventArgs e)
{

    string url = @"http://www.veranomovistar.com.pe/";
    System.Net.WebClient wc = new System.Net.WebClient();
    HtmlDocument doc = new HtmlDocument();
    doc.Load(wc.OpenRead(url));

    var metaTags = doc.DocumentNode.SelectNodes("//title");

    if (metaTags != null)
    {
        string title = metaTags[0].InnerText;
    }
}

有任何疑问,发表您的评论。

答案 1 :(得分:0)

在较高级别,您只需要将标准HTTP请求发送到所需的URL。这将为您提供网站的标记。然后,您可以检查标记(通过将其解析为DOM对象,然后查询DOM,或者通过运行一些简单的正则表达式/模式匹配来查找您感兴趣的内容)来提取文档的<title>之类的内容。元素和页面上的任何<img>元素。

答案 2 :(得分:0)

在我的脑海中,我会使用HttpWebRequest来获取页面并自己解析标题,然后使用更多的HttpWebRequests来获取页面上引用的任何图像。尽管有一个更好的方法可以做到这一点有一个很好的机会,有人会来并告诉你它是什么。如果没有,它看起来像这样:

HttpWebResponse response = null;
try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(<your URL here>);
    response = (HttpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    //use the StreamReader object to get the page data and parse out the title as well as
    //getting locations of any images you need to get
catch
{
    //handle exceptions
}
finally
{
    if(response != null)
    {
        response.Close();
    }
}

这可能是愚蠢的做法,但这是我的0.02美元。

答案 3 :(得分:-1)

你可以在源体上使用javascript编写

例如

如果你使用母版页,你就可以在matser页面上编写反映在所有页面上的代码 你也可以像这样使用这个脚本中的图像Url属性

khan mohd faizan