2011-07-07 24 views
0

我被困在这个问题上:我需要从页面presnycas.eu(要同步)获取时间和日期。日期很好,但我无法得到时间。问题是,当我打电话给IdHTTP.Get(..)方法,结果我得到了页面的HTML,但时间不见了。就像这样:如何获得INDY的内部HTML

<div class="boxik"> 
<table style="text-align: left; width: 700px; height: 116px;" border="0" cellpadding="2" cellspacing="0"> 
    <tbody> 
    <tr> 
     <td style="width: 400px;" colspan="1" rowspan="5"> 
      <div class="hodinyhlavni"> 

      <span id="servertime"></span> 
       // This is where the time should be - when viewed with 
       // developer tools in Chrome, it does show the time 
       // (picture here http://img684.imageshack.us/img684/166/pagem.png) 
      </div> 
     </td> 
     <td style="width: 0px;"> &nbsp;  
      07.07.2011 
     </td> 

现在我使用的是尴尬的方式 - 我打开一个TWebBrowser,然后调用

Time:=StrToTime(WebBrowser1.OleObject.Document.GetElementByID('servertime').innerhtml); 

但嗯,这是相当缓慢的,我宁愿不是在所有使用TWebBrowser。

那么,如何通过函数调用来获取元素的innerhtml?

在此先感谢

+0

时间是使用JavaScript函数(displaytime),这是产生也用于显式修改servertime HTML元素。然后每秒钟调用这个函数。据我所知,你将无法阅读那个时间。 – LightBulb

回答

1

这个答案的最重要的部分是“你需要了解HTML和JavaScript,并找出该网站是如何工作的。”打开网站,右键单击并执行“显示源”。你会发现顶端有这样的:

<script type="text/javascript">var currenttime = 'July 07, 2011 11:51:14'</script> 

那样子的时候,在我的情况下,时间是正确的,但没有调整到我的时区。您可以使用Indy轻松获取纯HTML,显然这就够了。此快速代码示例向您展示了如何使用一小段RegEx获取HTML并解析日期和时间。如果您使用的是Delphi XE,则必须将TPerlRegEx类名称和PerlRegEx单元名称替换为任何XE所需的名称。如果您使用的是旧版Delphi,那么不能使用RegEx!下载TPerlRegEx,它是免费的,并与XE的东西兼容。

program Project29; 

{$APPTYPE CONSOLE} 

uses 
    SysUtils, IdHTTP, PerlRegEx, SysConst; 

function ExtractDayTime: TDateTime; 
var H: TIdHTTP; 
    Response: string; 
    RegEx: TPerlRegEx; 

    s: string; 

    Month, Year, Day, Hour, Minute, Second: Word; 
begin 
    H := TIdHttp.Create(Nil); 
    try 
    Response := H.Get('http://presnycas.eu/'); 
    RegEx := TPerlRegEx.Create; 
    try 
     RegEx.RegEx := 'var\ currenttime\ \=\ \''(\w+)\ (\d{1,2})\,\ (\d{4})\ (\d{1,2})\:(\d{1,2})\:(\d{1,2})\'''; 
     RegEx.Subject := Response; 
     if RegEx.Match then 
     begin 

      // Translate month 
      s := RegEx.Groups[1]; 
      if s = SShortMonthNameJan then Month := 1 

      else if s = SShortMonthNameFeb then Month := 2 
      else if s = SShortMonthNameMar then Month := 3 
      else if s = SShortMonthNameApr then Month := 4 
      else if s = SShortMonthNameMay then Month := 5 
      else if s = SShortMonthNameJun then Month := 6 
      else if s = SShortMonthNameJul then Month := 7 
      else if s = SShortMonthNameAug then Month := 8 
      else if s = SShortMonthNameSep then Month := 9 
      else if s = SShortMonthNameOct then Month := 10 
      else if s = SShortMonthNameNov then Month := 11 
      else if s = SShortMonthNameDec then Month := 12 

      else if s = SLongMonthNameJan then Month := 1 
      else if s = SLongMonthNameFeb then Month := 2 
      else if s = SLongMonthNameMar then Month := 3 
      else if s = SLongMonthNameApr then Month := 4 
      else if s = SLongMonthNameMay then Month := 5 
      else if s = SLongMonthNameJun then Month := 6 
      else if s = SLongMonthNameJul then Month := 7 
      else if s = SLongMonthNameAug then Month := 8 
      else if s = SLongMonthNameSep then Month := 9 
      else if s = SLongMonthNameOct then Month := 10 
      else if s = SLongMonthNameNov then Month := 11 
      else if s = SLongMonthNameDec then Month := 12 

      else 
      raise Exception.CreateFmt('Don''t know what month is: %s', [s]); 

      // Day, Year, Hour, Minute, Second 
      Day := StrToInt(RegEx.Groups[2]); 
      Year := StrToInt(RegEx.Groups[3]); 
      Hour := StrToInt(RegEx.Groups[4]); 
      Minute := StrToInt(RegEx.Groups[5]); 
      Second := StrToInt(RegEx.Groups[6]); 

      Result := EncodeDate(Year, Month, Day) + EncodeTime(Hour, Minute, Second, 0); 

     end 
     else 
     raise Exception.Create('Can''t get time!'); 
    finally RegEx.Free; 
    end; 
    finally H.Free; 
    end; 
end; 

begin 
    WriteLn(DateTimeToStr(ExtractDayTime)); 
    ReadLn; 
end. 
+0

我对HTML和JS有一点了解,是真的。我完全忽略了标题:(谢谢你辛苦的回答:) –

1

我想你指定的链接(http://presnycas.eu/),并从HTML我可以看到,实际时间是在HTML另一个位置返回,再后来如果你想同步,你需要周期性地“抓取”新的时间。在HTML(head元素中)

寻找此:

<head> 
... 
<script type="text/javascript">var currenttime = 'July 07, 2011 12:01:26'</script> 
... 
</head> 
0

如何利用Indy TidHTTP

var 
    Form2: TForm2; 
    xpto:tmemorystream; 
    xx:string; 
    implementation 

{$R *.fmx} 

procedure TForm2.Button1Click(Sender: TObject); 

begin 
xpto:=tmemorystream.Create; 
idhttp1.Get('http://google.com',xpto); 
xpto.Position:=0; 

end; 


procedure TForm2.IdHTTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode); 
var x:string; 
begin 

SetString(x, PAnsiChar(xpto.Memory), xpto.Size); 

memo1.Lines.add(x); 
end; 

// 对于Android的Firemonkey使用得到内部HTML请MarshaledAString取代Pansichar