我如何从字符串 C# 中获取始终变化的值

发布于 2024-08-12 18:25:14 字数 410 浏览 8 评论 0原文

我正在开发一个程序,它将自动从哇军械库获取你的角色统计数据和诸如此类的东西。我已经有了 html,并且我可以识别字符串在哪里,但我需要获取“this. effective”值,在本例中为 594。但由于它总是在变化(其他值也是如此,我不能只要采取一定的立场,我们将不胜感激,

谢谢马特 --------- 这是 html 片段:

    function strengthObject() {
        this.base="168";
        this.effective="594";
        this.block="29";
        this.attack="1168";

this.diff=this. effective - this.base;


I am working on a program that will automatically get your characters stats and whatnot from the wow armory. I already have the html, and i can identify where the string is, but i need to get the "this.effective" value, which in this case is 594. But since its always changing (and so are the other values, i cant just take it a certain position. Any help would GREATLY appreciated.

Thanks

Matt
--------- This is the html snippet:

    function strengthObject() {
        this.base="168";
        this.effective="594";
        this.block="29";
        this.attack="1168";

this.diff=this.effective - this.base;


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-08-19 18:25:14

您可以使用正则表达式来做到这一点:

using System;
using System.Text.RegularExpressions;

class Program
{
    public static void Main()
    {
        string html = @"        function strengthObject() {
                this.base=""168"";
                this.effective=""594"";
                this.block=""29"";
                this.attack=""1168"";";

        string regex = @"this.effective=""(\d+)""";

        Match match = Regex.Match(html, regex);
        if (match.Success)
        {
            int effective = int.Parse(match.Groups[1].Value);
            Console.WriteLine("Effective = " + effective);
            // etc..
        }
        else
        {
            // Handle failure...
        }
    }
}

You can do it using regular expressions:

using System;
using System.Text.RegularExpressions;

class Program
{
    public static void Main()
    {
        string html = @"        function strengthObject() {
                this.base=""168"";
                this.effective=""594"";
                this.block=""29"";
                this.attack=""1168"";";

        string regex = @"this.effective=""(\d+)""";

        Match match = Regex.Match(html, regex);
        if (match.Success)
        {
            int effective = int.Parse(match.Groups[1].Value);
            Console.WriteLine("Effective = " + effective);
            // etc..
        }
        else
        {
            // Handle failure...
        }
    }
}
彼岸花似海 2024-08-19 18:25:14

从 XML 版本的网站中提取信息要容易得多。

如果您向这样的 URL 发出请求(仅使用有效的字符名称),那么您将返回一个 XML 文档,您可以使用 XML 解析器轻松提取数据。

http://eu.wowarmory.com/character-sheet。 xml?r=Nordrassil&cn=Someone

URL 与您在网络浏览器中看到的 URL 相同。

请注意,您必须将请求的用户代理字段设置为支持 XML 版本文件的受支持浏览器的字段,否则您将返回 HTML。我在程序中使用“Mozilla/5.0 Firefox/2.0.0.1”作为用户代理,并且工作正常。

哦,也不要在一秒钟内发出超过几个请求,或者平均每 3 或 4 秒发出一个以上请求,否则网站会阻止您的 IP 几个小时......

It's much easier to extract the information from the XML version of the website.

If you make a request to a URL like this (Only with a valid character name) then you get back an XML document that you can use an XML parser to easily extract the data.

http://eu.wowarmory.com/character-sheet.xml?r=Nordrassil&cn=Someone

The URLs are the same as the ones you see in your web browser.

Please note though that you MUST set the User Agent field of the request to be that of a supported browser that supports the XML version of the file or you get back HTML instead. I use "Mozilla/5.0 Firefox/2.0.0.1" as the user agent in my program and it works fine.

Oh, also don't make more than a few requests in second, or an average of more than one request every 3 or 4 seconds or the site blocks your IP for a few hours ...

深空失忆 2024-08-19 18:25:14

一种方法是使用正则表达式从 HTML 源中提取该值:

this.effective="(\d+)"

请注意,HTML 抓取不是理想的解决方案(例如,当 HTML 格式更改时它可能会中断),但是我不知道“哇军械库”以及还有哪些其他方式可以获取此信息。

One way would be to use a regular expression to extract this value from the HTML source:

this.effective="(\d+)"

Note that HTML scraping is not an ideal solution (for example, it may break when the format of the HTML changes) however I don't know about the "wow armory" and what other ways there are to get this information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文