我正在构建一个Windows手机游戏数据库。我想刮掉那些数据。但我找不到任何库或api或文档。
请不要与此重复此问题:windows store api to access metro and phone apps information
因为该答案仅与Windows PC应用有关,而不是Windows手机应用。
答案 0 :(得分:2)
这就是你如何获得最受欢迎的游戏:
下载和解析部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace TestAPI
{
public class WindowsPhoneGamesScraper
{
static string[] categories = new string[]
{
"action-adventure/actionandadventure",
"card-board/cardandboard",
"classics/classics",
"educational/educational",
"family/family",
"music/music",
"platformer/platformer",
"puzzle-trivia/puzzleandtrivia",
"racing-flying/racingandflying",
"role-playing/roleplaying",
"shooter/shooter",
"sports-recreation/sportsandrecreation",
"strategy-simulation/strategy",
"xbox-companion/xboxcompanion"
};
static string[] types = new string[] { "xbox-games", "top-free-games", "new-games", "top-paid-games", "top-rated-games" };
public static async Task<List<WindowsPhoneAppInfo>> GetWindowsPhoneApps(string store, Action<string> updateStatus)
{
return await GetWindowsPhoneApps(store, categories.ToList(), types.ToList(), updateStatus);
}
public async static Task<List<WindowsPhoneAppInfo>> GetWindowsPhoneApps(List<string> categories, List<string> app_types, Action<string> updateStatus)
{
return await GetWindowsPhoneApps("en-US", categories, app_types, updateStatus);
}
public async static Task<List<WindowsPhoneAppInfo>> GetWindowsPhoneApps(string store, List<string> categories, List<string> app_types, Action<string> updateStatus)
{
List<WindowsPhoneAppInfo> data = new List<WindowsPhoneAppInfo>();
foreach (string category in categories)
{
foreach (string type in app_types)
{
updateStatus(string.Format("Downloading Category {0} and Type{1} Total:{2}", category, type, data.Count));
data.AddRange(await ScrapeAppsFromUrl("http://www.windowsphone.com/" + store + "/store/" + type + "/" + category, true));
}
}
return data;
}
public async static Task<List<WindowsPhoneAppInfo>> GetNewWindowsPhoneGames(string store, string category = "")
{
List<WindowsPhoneAppInfo> data = new List<WindowsPhoneAppInfo>();
data.AddRange(await ScrapeAppsFromUrl("http://www.windowsphone.com/" + store + "/store/new-games/" + category, false));
return data;
}
public async static Task<List<WindowsPhoneAppInfo>> GetFeaturedWindowsPhoneGames(string store)
{
List<WindowsPhoneAppInfo> data = new List<WindowsPhoneAppInfo>();
data.AddRange(await ScrapeAppsFromUrl("http://www.windowsphone.com/" + store + "/store/featured-games", false));
return data;
}
private async static Task<List<WindowsPhoneAppInfo>> ScrapeAppsFromUrl(string p, bool handlePaging)
{
List<WindowsPhoneAppInfo> data = new List<WindowsPhoneAppInfo>();
string res = "";
WebClient client = new WebClient();
try
{
TaskCompletionSource<string> tcsDownloadedText = new TaskCompletionSource<string>();
client.DownloadStringCompleted += (sen, ev) =>
{
if (ev.Error != null)
tcsDownloadedText.TrySetResult("");
else
tcsDownloadedText.TrySetResult(ev.Result);
};
client.DownloadStringAsync(new Uri(p, UriKind.Absolute));
res = await tcsDownloadedText.Task;
if (string.IsNullOrEmpty(res))
return data;
}
catch (Exception x) { }
try
{
var list = res.Split(new[] { "<td class=\"small\">" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in list.Skip(1).ToList().Take(list.Length - 2))
{
try
{
var xelem = XElement.Parse("<td class=\"small\">" + item.Replace("</tr>", "").Replace("<tr>", ""));
WindowsPhoneAppInfo wpai = new WindowsPhoneAppInfo(xelem);
data.Add(wpai);
}
catch (Exception e)
{
//Log the errors if you need
}
}
if (res.Contains(@"id=""nextLink""") && handlePaging)
{
var lista = res.Split(new string[] { "id=\"nextLink\"" }, StringSplitOptions.RemoveEmptyEntries);
string url = "http://www.windowsphone.com" +
(lista[1].Split(new string[] { "\"" }, StringSplitOptions.RemoveEmptyEntries)[1]);
List<WindowsPhoneAppInfo> apps = await ScrapeAppsFromUrl(url, handlePaging);
data.AddRange(apps);
}
}
catch (Exception e)
{
//Log the errors if you want
}
return data;
}
}
public class WindowsPhoneAppInfo
{
public WindowsPhoneAppInfo(XElement xelem)
{
var elements = xelem.Elements("a").ToList();
Title = elements[1].Value;
var xAttribute = elements[1].Attribute("href");
if (xAttribute != null)
{
Url = xAttribute.Value;
AppID = Url.Split(new[] { '/' }).Last();
}
var imageLogo = xelem.Descendants("img").FirstOrDefault();
if (imageLogo != null)
{
var logoURLAttribute = imageLogo.Attribute("src");
if (logoURLAttribute != null)
LogoURL = logoURLAttribute.Value;
}
if (xelem.Descendants("div")
.ToList()
.Any(
elem => elem.Attributes("class").ToList().Any(att => att.Value.Contains("ratingSmall"))))
Rating =
xelem.Descendants("div")
.ToList()
.First(
elem => elem.Attributes("class").ToList().Any(att => att.Value.Contains("ratingSmall")))
.Attributes("class")
.First()
.Value.Split(new[] { ' ' })
.Last();
Price = xelem.Descendants("div").Last().Value;
}
public string AppID { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string LogoURL { get; set; }
private ImageSource _logo;
public ImageSource Logo
{
get
{
if (_logo == null)
_logo = new BitmapImage(new Uri(LogoURL, UriKind.Absolute));
return _logo;
}
}
public string Rating { get; set; }
public string Price { get; set; }
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((WindowsPhoneAppInfo)obj);
}
protected bool Equals(WindowsPhoneAppInfo other)
{
return string.Equals(AppID, other.AppID);
}
public override int GetHashCode()
{
return (AppID != null ? AppID.GetHashCode() : 0);
}
}
}
样本:
List<WindowsPhoneAppInfo> data = await WindowsPhoneGamesScraper.GetFeaturedWindowsPhoneGames("en-US");//, categories.ToList(), types.ToList());
data.AddRange(await WindowsPhoneGamesScraper.GetWindowsPhoneApps("en-US", UpdateAction));
data = data.Distinct().ToList();
lboGames.ItemsSource = data;
您可以使用所有受支持的文化调用相同的操作。