如何从网站获取特定的单词/数字
本文关键字:单词 数字 网站 获取 | 更新日期: 2023-09-27 18:29:37
我想获得游戏中玩家的统计数据,并在我的应用程序中显示这些数据。我需要的统计数据放在网站上(account.xxx.com/player/username)。
如何从该网站获取(例如)致死率?
我必须强调的第一件事是:
确保您以这种方式使用他们的数据不违反网站的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);
};
正如其他人所建议的那样,你应该看看如何提出一个好问题。要求别人帮你解决问题而不展示你自己的尝试,通常被认为是不礼貌的。
屏幕将页面刮成代码,然后遍历生成的标记以获得所需的值。例如正则表达式、DOM解析、Linq到Xml等。。。
"屏幕抓取"是将页面调用为变量中的代码,而不是在浏览器上呈现。一旦您在代码中将页面作为变量,您就可以随心所欲地对其进行操作。
使用HtmlAgilityPack从html 中提取所需信息