开发者

How to grab the first image from page url and display it along BOOKMARK description? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am developing a social bookmarking website in ASP.NET and in it I am displaying bookmarks which users add. I want to put the fist image of web page along with bookmarks to display to user. but I don't know how I get that .

Update (14.09.11): Here's that solution I found:

(HttpWebRequest) HttpWebRequest.Create(TextBox1.Text); 
request.UserAgent = "LPU Crawler"; 
WebResponse response = request.GetResponse(); 
Stream stream = response.GetResponseStream(); 
StreamReader reader = new StreamReader(stream); 
string httptxt = reader.ReadToEnd(); 
extractimgs(httptxt); 
foreach (string pic in pics) 
{ 
    TextBox2.Text += pic; 
    TextBox2.Text += "\n"; 
} 

private void extractimgs(string httptxt) 
{ 开发者_JS百科
    const string match=
        "(?<=img\\s+src\\=[\x27\x22])(?<Url>[^\x27\x22]*)(?=[\x27\x22])"; 
    MatchCollection matches = Regex.Matches(httptxt,match,
        RegexOptions.IgnoreCase); 
    for (int i = 0; i <= matches.Count - 1; i++) 
    { 
        Match anchorMatch = matches[i]; 
        if (String.IsNullOrEmpty(anchorMatch.Value)) 
        { 
            Response.Write("No Img Found"); 
        } 
        pics.Add(anchorMatch.ToString()); 
    } 
}


You could use the HTML agility pack.

You can download it via CodePlex:

http://htmlagilitypack.codeplex.com/

Or you can use NuGet:

http://nuget.org/List/Packages/HtmlAgilityPack

Using the Html Agility Pack you can easily download a web page and parse its contents.

To retrieve the URL of the first image you can use the following LINQ query:

var url = "http://www.stackoverflow.com";
var document = new HtmlWeb().Load(url);
var imageUrl = (from image in document.DocumentNode.Descendants("img")
                where !String.IsNullOrEmpty(image.GetAttributeValue("src", null))
                select image.Attributes["src"].Value).FirstOrDefault();
if (imageUrl != null)
{
   //...
}

You can use the address of the image(s) (SRC attribute) to include them in your own page or to issue a web request to download them.

Some quick code to download an image:

string imageUrl= 
    "http://www.example.com/logo.jpg";
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Image image = Image.FromStream(response.GetResponseStream());
var extension = Path.GetExtension(url).Substring(0, 4);
image.Save(@"c:\test" + extension);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜