如何从网站获取特定的单词/数字?

时间:2016-01-10 12:47:18

标签: c# visual-studio

我想在游戏中获取玩家的统计数据,并在我的应用程序中显示这些数据。 我需要的统计数据在网站上(account.xxx.com/player/username)上线。

如何从该网站获取(例如)杀死率

3 个答案:

答案 0 :(得分:2)

Screen scrape将页面转换为代码,然后遍历生成的标记以获取所需的值。例如。正则表达式,DOM解析,Linq到Xml等,......

“屏幕抓取”是将页面调用代码到变量中而不是渲染到浏览器上的行为。一旦您将代码中的页面作为变量,您就可以根据需要对其进行操作。

答案 1 :(得分:1)

我必须强调的第一件事是:

确保您不会以这种方式使用其数据来反对网站的ToS。

所以,例如:

// Store the URL of the website you are looking at.
string path = "http://euw.op.gg/summoner/userName=";
string userName = "froggen";
string url = path + userName;
// Create a new WebClient to download the html page.
using (WebClient client = new WebClient())
{
    // Download the html source code of the user page.
    string html = client.DownloadString(url);

    // Finding the kda depends on the website - you need to know what you are looking for. Have a look at the page source and see where the kda is stored.
    // I've had a look at the source of my example and I know kda is stored in <span class="KDARatio">3.92:1</span>.

    // You'll need a way to get that data out of the HTML - you might try to parse the file and traverse it.
    // I've chosen Regex to match the data I'm looking for.
    string pattern = @"<span class=""KDARatio"">\d+\.\d+";
    // I take the first string that matches my regex from the document.
    string kdaString = Regex.Matches(html, pattern)[0].Value;
    // I trim the data I don't need from it.
    string substring = kdaString.Substring(kdaString.IndexOf('>') + 1);
    // Then I can convert it into a double - giving me the x:1 kda ratio for this player.
    double kda = double.Parse(substring);
};

正如其他人所建议的那样,你应该看看how to ask a good question。一般认为要求人们为你解决问题而不展示你自己尝试过的东西是不礼貌。

答案 2 :(得分:0)

使用HtmlAgilityPack从html中提取您需要的信息