0

我想从 c# net 中的网站获取 JavaScript 变量的值。目前我使用这个代码:

System.Net.WebClient wc = new System.Net.WebClient();

string webData = wc.DownloadString("://87.126.167.13/");

textBox1.Text = Convert.ToString(webData);

它返回 HTML。但我想要这个链接的温度值: ://87.126.167.13/ 但它返回:

<td>Temperature:</td><td><script>document.write(MNS,TW1,TW2,'.',TF,TF1)</script> C</td>

4

1 回答 1

0

The CSV page format (http://87.126.167.13/x) that website has would probably be more useful to you. It looks like:

Date,AirTemp,SoilTemp,Humidity
2013-08-22 14:46:00,+31.20,+21.6,32
2013-08-22 15:13:55,+32.22,+21.5,31
** more values **
2013-08-22 13:50:26,+30.39,+21.6,34
2013-08-22 14:18:19,+31.40,+21.6,33
1004+2200

You could parse these values with some simple code, like

var lines = webData.Split('\n');
var allData = lines.Skip(1).Take(lines.Length - 2).Select(x => new WeatherData(x));
var latestDate = allData.Max(x => x.DateTime);
var latestAirTemp = allData.First(x => x.DateTime == latestDate).AirTemp;

public class WeatherData
{
    public DateTime DateTime { get; set; }
    public decimal AirTemp { get; set; }
    public decimal SoilTemp { get; set; }
    public int Humidity { get; set; }
    public WeatherData(string rawData)
    {
        // implement
    }
}

That or the script file http://87.126.167.13/s, which is what defines the MNS,TW1,... that you mention. From that, you could parse out what value(s) you want. It looks like:

var AN3=   10;var MNS="+";var TW1=    1;var TW2=    9;var TF=    7;var TF1=    0;var HR=   22;var MN=   23;var SC=   39;var DY=   22;var MH=    8;var YR=    3;var RH="   53";var WD="South-East";var WS="00";var MNS2="+";var ST="21.5";var BP="8261";var RAD2="1.11";
于 2013-08-22T19:22:55.643 回答