如何使用c#在html中更改选定的值?
问题描述:
即时通讯尝试从网页获取一些数据。即时通讯编程在C#.net中。该网页有一个下拉列表(或组合框),就像我下面显示的那样。数据根据所选下拉列表项目而改变,但url不会更改。我的问题是我的代码如何更改选定的值并从网页获取数据?我分析,并得到了唯一的项目像一个:如何使用c#在html中更改选定的值?
**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));**
<跨度ID = “spImsak”> 02:44 </SPAN>
我下载网页的HTML代码作为字符串。搜索“spImsak”。最后我得到了“02:44”作为一个字符串。我想为所有组合框项目做。你能给我什么建议吗?
样本网页:http://www.diyanet.gov.tr/
红色的是组合框。黄色的是我想要得到的数据。
答
我想接着网页的网络,发现当我点击任何下拉列表元素,网页与运行参数的Web服务。我解释了如何将其应用于我的问题。
web service and parameters image
所有我需要发送POST请求这些参数这个Web服务,得到了串(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);
}
}
为了阅读html字符串的片段,请看一下HtmlAgilityPack。然后你可以通过它的id找到该跨度。 –
以这种方式下载HTML页面时,您会从服务器获取全新副本,而不是* * *浏览器中的任何副本。此外,它只是一个字符串,不知道任何关于组合框等 –
@HansKesting我的公司不想使用第三方库。 –