无法从跨度获取正确的数据 [英] Not getting correct data from span

查看:63
本文介绍了无法从跨度获取正确的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为Jessecar的SteamBot创建自定义用户处理程序,该处理程序与我遇到的问题无关,但从本质上讲,我已经做到了,因此您可以将机器人设置为播放特定游戏的App ID,而我一直在使用它来闲置Steam交易卡的游戏,唯一的问题是,我可以检查其是否完成的唯一方法是检查其库存以及应该丢弃多少张卡,这并不是很麻烦,但是我创建它的主要原因是为了提高效率,并且每次这样做都违背了它的目的.

I've been making a custom user handler for Jessecar's SteamBot, which is unrelated to the problem I'm having, but essentially what I've done, is I've made it so you can set the bot to play a specific game by App ID, and I've been using this to idle on games for Steam Trading Cards, the only issue is, the only way I can check if it's finished, is by checking its inventory and how many cards are supposed to drop, which isn't too much of a hassle, but the main reason I created this was for efficiency, and doing this every time kind of defeats the purpose of it.

因此,我尝试从正在玩的游戏中的机器人的徽章页面获取数据,这就是我到目前为止所拥有的...

Because of this, I tried getting data from the badge page for the bot on the game that it's playing, this is what I have so far...

else if (message.StartsWith(".updateidle"))
            {
                var webGet = new HtmlWeb();
                var SteamID64 = Bot.SteamClient.SteamID.ConvertToUInt64();
                string htmlget = "http://www.steamcommunity.com/profiles/" + SteamID64 + "/gamecards/" + newgame;
                var doc = webGet.Load(htmlget);

                HtmlNode hoursNode = doc.DocumentNode.SelectSingleNode("//div[@class=\"badge_title_stats_playtime\"]");
                string hours = Regex.Match(hoursNode.InnerText, @"[0-9\.,]+").Value;

                var cards = doc.DocumentNode.SelectSingleNode("div[@class='badge_title_stats_drops']/span").InnerText;



                if (hours == string.Empty)
                {
                    hours = "0.0";
                }

                Bot.SteamFriends.SendChatMessage(OtherSID, type, "I have been idling for " + hours + " hours on game " + newgame + " and have " + cards + " card drops remaining.");
            }

获取时间工作正常,如果该机器人没有时间在游戏上,则它不会出现,因此我只是检查它是否为空,然后将其设置为0.0,但是,在纸牌上,它显示为否卡剩余的卡数"或卡剩余的卡数"都没有,我尝试使用与小时数相同的方法,但只有当它是数字时才得到它,并且它仍返回"0",因此结果相同...

Getting the hours works fine, if the bot has no time on that game, it doesn't appear, so I just check if it's empty then set it to 0.0, however, with the cards, it appears as either "No card drops remaining" or " card drops remaining" which it doesn't get either, I tried using the same method as the hours and only get it if it's a number, and it still returns with "0", same result goes for this...

我也再次尝试检查字符串是否为空,因为这可能意味着没有剩余的掉卡次数,因为将没有数字,并且我也在线寻找了获取跨区数据的方法. div或span数据常规,并且这两种方法都不起作用,它们只会返回"0".而且,如果您还不能确定的话,那么我确实有HTML Agility Pack.

I also tried again with doing a check if the string is empty, because that could mean there is no card drops remaining, as there would be no numbers, and I also had a look online for methods of getting span data inside a div, or span data general, and neither methods worked, they'd just return with "0". And if you can't already tell, I do have the HTML Agility Pack.

推荐答案

因此,在上一个答案中,我决定不进行编辑,因为此处的后续操作将很大.我为此同时使用了Selenium和Html Agility Pack.首先,我使用Selenium登录(我正在使用Mono btw).之后,我手动输入对我的PC进行授权(如果您的PC已被授权,则跳过此步骤),然后转到控制台并按任意键以继续获取卡信息.在这种情况下,我将收集所有游戏的卡信息.由于无法实施,我无法确定哪个游戏仍有掉牌.

So building in my previous answer, that I have decided not to edit, since the followup here is gonna be large. I amusing both Selenium and Html Agility Pack for this. First I log in using Selenium(I am using Mono btw). After that I type in authorize my pc manually(if yours is already authorized then skip this step) and then go to the console and press any key to proceed with getting card info. I will gather the card info for all games in this case. I can't identify which game still has card drops as it has not been implemented yet.

class MainClass
{


    public static void Main(string[] args)
    {
        string userName = "username";
        string password ="password";
        string steamProfile = "steamprofile";
        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();


        using (var driver = new FirefoxDriver())
        {

            // Go to the home page
            driver.Navigate().GoToUrl("https://store.steampowered.com//login/?redir=0");

            // Get the page elements
            var userNameField = driver.FindElementById("input_username");
            var userPasswordField = driver.FindElementById("input_password");
            //var loginButton = driver.FindElementById("login_btn_signin");
            var loginButton = driver.FindElementByXPath("//button[@class='btnv6_blue_hoverfade  btn_medium']");

            // Type user name and password
            userNameField.SendKeys(userName);
            userPasswordField.SendKeys(password);

            // and click the login button
            loginButton.Click();
            System.Threading.Thread.Sleep(5000);

            //Type authorization code and enter manually.

            System.Console.ReadKey();

            driver.Navigate().GoToUrl("http://steamcommunity.com/profiles/"+steamProfile+"/badges");


            driver.GetScreenshot().SaveAsFile(@"screen.png", ImageFormat.Png); //Debuggin purposes, as I was first using PhantomJS


            htmlDoc.LoadHtml(driver.PageSource);

            Console.Clear();

        }

        HtmlNodeCollection col = htmlDoc.DocumentNode.SelectNodes("//span[@class='progress_info_bold']");

        foreach (HtmlNode n in col)
        {
            Console.WriteLine(n.InnerText);
        }
    }
}

}

我的情况下的输出

5 of 29 tasks completed
No card drops remaining
No card drops remaining
No card drops remaining
4 card drops remaining
3 card drops remaining

此代码还为您提供了徽章进度.您必须弄清楚如何在HTML Agility Pack中过滤数据(在

This code also gives you the badge progress. You must figure out yourself how to filter your data in Html Agility Pack(read up on xpath). I also recommend that you use Selenium, since you can start a steamgame from your webpage using it.

请记住,我在第一个答案中给出的xpath也在上面的代码中使用过,它找到的ALL("//")的类具有等于"progress_info_bold"的类.

remember that the xpath I gave you in my first answer and is also used in the code above finds ALL("//") the that has a class that equals "progress_info_bold".

这篇关于无法从跨度获取正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆