【问题标题】:how to change selected value in html using c#?如何使用 c# 更改 html 中的选定值?
【发布时间】:2017-11-22 08:42:33
【问题描述】:

我试图从网页中获取一些数据。我在 c# .net 中编写代码。该网页有一个下拉列表(或组合框),如下所示。数据根据选定的下拉列表项更改,但 url 不会更改。我的问题是我的代码如何更改选定的值并从网页获取数据?我只解析并得到了这样的一项:

        **WebClient wc = new WebClient();
        string kaynak = wc.DownloadString("http://www.diyanet.gov.tr/");
        string imsak = "spImsak";
        int imindex = kaynak.IndexOf(imsak);
        imindex += 9;
        System.Console.WriteLine(kaynak.Substring(imindex, 5));**

02:44

我将网页的 html 代码下载为字符串。搜索“spImsak”。最后我得到了“02:44”作为字符串。我想为所有组合框项目做这件事。你能给我什么建议吗?

示例网页:http://www.diyanet.gov.tr/

红色的是组合框。黄色的是我想要获取的数据。

【问题讨论】:

  • 要读取 html 字符串的片段,请查看 HtmlAgilityPack。然后你可以通过它的id找到那个span。
  • 当您以这种方式下载 HTML 页面时,您会从服务器获得一个新副本,而不是 您的 浏览器中的任何内容。另外,它只是一个对组合框等一无所知的字符串。
  • @HansKesting 我的公司不想使用第三方库。
  • @HansKesting 我知道我有一个字符串。你有什么解决办法吗?

标签: c# html combobox webclient


【解决方案1】:

我跟踪了网页的网络,发现当我单击任何下拉列表元素时,网页会运行带有参数的 Web 服务。我解释了如何将其应用于我的问题。

web service and parameters image

我只需要使用这些参数向这个 Web 服务发送一个 POST 请求并获得字符串 (json)。我是按照 c# 代码做到的。

using (WebClient client = new WebClient())
        {
            int turn;
            byte[] response;
            string result;
            /* gets response for 81 city */
            for (turn = 500; turn < 581; ++turn)
            {

                response =
                client.UploadValues("http://diyanet.gov.tr/PrayerTime/MainPrayerTimesSet", new NameValueCollection()
                {
                    { "countryName", "2" },
                    { "name", turn.ToString() }
                });
                /* without sleep, web service does not response successive requests */
                System.Threading.Thread.Sleep(5);

                /* turns incoming byte[] -> string */ 
                result = System.Text.Encoding.UTF8.GetString(response);

                Console.WriteLine(result);
            }
        }

【讨论】:

    猜你喜欢
    • 2014-03-16
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多