C#WebClient-如何找到类的“ src”?

问题描述

我取得了进步:

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);
}

在线*,有人知道吗,我如何在其中添加“ src”? 我给你一张图片

enter image description here

解决方法

由于 img 标签没有更好的方法可以轻松地找到它,因此我首先使用类'profile_avatar_frame'查找div,然后进入父节点并找到 URL ,如下所示:

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);