Hello everyone,

I'm trying to get some text via a C# console app from a web page. The text I'm trying to get is the current players from our Battlefield 3 server we are renting.

The url is: http://battlelog.battlefield.com/bf3/servers/show/bcc85e8a-c2fd-41a4-98f4-21548297a9b1/

Obvious, the text I need is this: http://prntscr.com/4qbiz

I have some code that works, but I was wondering if there was a simpler way to do this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;

namespace bf3server
{
    class Program
    {
        private const string url =
        "http://battlelog.battlefield.com/bf3/servers/show/bcc85e8a-c2fd-41a4-98f4-21548297a9b1/";

        private const string regexTemplate = ">([^<>]*?{0}[^<>]*?)<";

        static void Main(string[] args)
        {
            WebClient client = new WebClient();

            string html = client.DownloadString(url);

            string players = "0 / 64";
           
            Regex regex = new Regex(string.Format(regexTemplate, players), RegexOptions.IgnoreCase);

            var matches = regex.Matches(html);

            foreach (Match match in matches) 
                Console.WriteLine("Current Players: " + match.Groups[1].Value);
        }
    }
}

I really don't want to have to write 64 player strings and 64 regex variables and 64 match variables. Is there some easier logic I could use to get the nunber of players, which constantly changes? Thanks to anyone who can help.

Recommended Answers

All 4 Replies

Why don't you loop?

thines01,

How would I loop through something like this?

First, how do you tell the difference in the page from user 1 to user 2?
The resason I ask is a lot of this looks dynamically generated.
That's not necessarily a problem, but I think I'm not seeing something because I don't have an account on the server.

Hey thines01, I've sent you a PM.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.