我想獲得遊戲中玩家的屬性,並在我的應用程序中顯示這些屬性。 我需要的統計數據放在網站上(account.xxx.com/player/username)。如何從網站獲取特定的單詞/數字?
如何從該網站抓取(例如)殺/死率?
我想獲得遊戲中玩家的屬性,並在我的應用程序中顯示這些屬性。 我需要的統計數據放在網站上(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);
};
正如其他人的建議,你應該看看how to ask a good question。一般認爲,要求人們爲自己解決問題而沒有證明自己曾經嘗試過什麼,這通常被認爲是不禮貌的。
Screen scrape將頁面轉換爲代碼,然後遍歷結果標記以獲取所需的值。例如。正則表達式,DOM解析,Linq到Xml等,...
「屏幕抓取」是將頁面代碼調入變量而不是渲染到瀏覽器的行爲。一旦你將代碼中的頁面作爲一個變量,你可以根據需要操作它。
使用HtmlAgilityPack拉你從HTML需要的信息
OP和upvoter可能應該重新訪問[ask]文章。問題顯示沒有嘗試,也沒有研究的跡象 – MickyD