如何使用c#更改html中的选定值?

时间:2017-06-20 01:18:39

标签: c# html combobox webclient

我试图从网页上获取一些数据。我在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));**

< span id =" spImsak"> 02:44< / span>

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

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

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

enter image description here

1 个答案:

答案 0 :(得分:0)

我跟进了网页的网络,看到当我点击任何下拉列表元素时,网页会运行带参数的网络服务。我解释了如何将其应用于我的问题。

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);
            }
        }