简体   繁体   中英

C# WebClient - how can I find a 'src' of a class?

I made this progress:

public static void PictureRequest()
{
    var url = @"https://steamcommunity.com/id/";
    HtmlWeb web = new HtmlWeb();

    Console.Write("Please give a steam ID: ");
    url = url + Console.ReadLine();

    var htmlDoc = web.Load(url);

    var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    Random r = new Random();
    string fileName = desktop + "\\download" + r.Next(1, 10000) + ".jpg";

    Console.WriteLine("Downloaded to: " + fileName);

    var adress =
    line* htmlDoc.DocumentNode.SelectSingleNode("//span[@class='playerAvatarAutoSizeInner']");

    WebClient client = new WebClient();
    client.DownloadFile(adress.InnerText, fileName);
}

At the line*, does someone know, how can I put an 'src' in it? I give you an image在此处输入图片说明

Since the img tag has no better way to find it easily, I first looked for the div with the class 'profile_avatar_frame' then I got into the parent node and found the URL you want, like the following:

var adress = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='profile_avatar_frame']");
var picture_node  = adress.ParentNode.SelectSingleNode("img");
WebClient client = new WebClient();
client.DownloadFile(picture_node.GetAttributeValue("src", ""), fileName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM