Einzelnen Beitrag anzeigen

Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.896 Beiträge
 
Delphi 12 Athens
 
#29

AW: Text einer Internetseite in Memo einlesen

  Alt 25. Mär 2025, 02:48
Es gibt einfach noch viel mehr Informationen, die enthalten sein können...
Deshalb wäre es ja, wie ich schon schrieb, sinnvoll, den Betreiber nach einer richtigen Schnittstelle zu fragen, statt die Seite auszulesen.

Ich habe das mal angepasst. Es macht aber wohl Sinn, dass du die Zeile selbst als String untersuchst (ParseGameLine), da du vermutlich so bald nicht die Regular Expression selbst anpassen kannst. Auch würde es helfen, wenn du einen Parser verwendest und so die Möglichkeiten behandeln kannst.

So funktioniert es jedenfalls erst einmal:
Delphi-Quellcode:
uses
  System.Generics.Collections, System.RegularExpressions, System.Net.HttpClientComponent, System.StrUtils;

type
  TGame = class
    Category: string;
    GameTime: string;
    Status: string;
    Opponent1, Opponent2: string;
    RedCards1, RedCards2: string;
    URL: string;
    MatchResult1, MatchResult2: string;
    MatchResultInfo: string;
  end;

function LoadHtml(const AUrl: string): string;
var
  NetHTTPClient: TNetHTTPClient;
begin
  NetHTTPClient := TNetHTTPClient.Create(nil);
  try
    Result := NetHTTPClient.get(AUrl).ContentAsString;
  finally
    NetHTTPClient.Free;
  end;
end;

function ParseGameLine(const AValue: string): TGame;
var
  RegEx: TRegEx;
  Match: TMatch;
begin
  RegEx := TRegEx.Create('<span(?: class="live")?>([0-9:+'']+)(?:<span[^>]*>(.*?)<\/span>)?<\/span>\s*([^<]+)(?:<img[^>]*class="rcard-(\d+)"[^>]*>)?\s*- ([^<]+)(?:<img[^>]*class="rcard-(\d+)"[^>]*>)?\s*<a href="([^"]+)" class="[^"]*">([0-9\-]+):([0-9\-]+)(.*?)<\/a>');
  Match := RegEx.Match(AValue);
  if Match.Success then
  begin
    Result := TGame.Create;
    Result.GameTime := Match.Groups[1].Value;
    if Match.Groups[2].Success then
      Result.Status := Match.Groups[2].Value;
    Result.Opponent1 := Match.Groups[3].Value;
    if Match.Groups[4].Success then
      Result.RedCards1 := Match.Groups[4].Value;
    Result.Opponent2 := Match.Groups[5].Value;
    if Match.Groups[6].Success then
      Result.RedCards2 := Match.Groups[6].Value;
    Result.URL := Match.Groups[7].Value;
    Result.MatchResult1 := Match.Groups[8].Value;
    Result.MatchResult2 := Match.Groups[9].Value;
    if Match.Groups[10].Success then
      Result.MatchResultInfo := Match.Groups[10].Value;
  end
  else
    Result := nil;
end;

procedure FindGames(const ATarget: TObjectList<TGame>);
const
  cContentsContainer = '<div id="score-data">';
  cHeaderStart = '<h4>';
  cHeaderEnd = '</h4>';
var
  HtmlContents, CurrentCategory, CurrentLine: string;
  Data: TArray<string>;
  CurrentGame: TGame;
  i: Integer;
begin
  HtmlContents := LoadHtml('https://m.flashscore.de/?d=-1');
  HtmlContents := HtmlContents.Substring(HtmlContents.IndexOf(cContentsContainer) + Length(cContentsContainer));
  Data := HtmlContents.Split(['<br />']);
  for i := 0 to High(Data) do
  begin
    CurrentLine := Data[i];
    if CurrentLine.StartsWith(cHeaderStart) then
    begin
      CurrentCategory := CurrentLine.Substring(Length(cHeaderStart), CurrentLine.IndexOf(cHeaderEnd));
      if CurrentCategory.IndexOf('<') > 0 then
        CurrentCategory := CurrentCategory.Substring(0, CurrentCategory.IndexOf('<')).Trim;
      CurrentLine := CurrentLine.Substring(CurrentLine.IndexOf(cHeaderEnd) + Length(cHeaderEnd));
    end;
    CurrentGame := ParseGameLine(CurrentLine);
    if Assigned(CurrentGame) then
    begin
      CurrentGame.Category := CurrentCategory;
      ATarget.Add(CurrentGame);
    end;
  end;
end;

procedure TForm288.Button1Click(Sender: TObject);
var
  CurrentGame: TGame;
  Games: TObjectList<TGame>;
  i: Integer;
begin
  Games := TObjectList<TGame>.Create(True);
  try
    FindGames(Games);
    for i := 0 to Games.Count - 1 do
    begin
      CurrentGame := Games[i];
      Memo1.Lines.Add(CurrentGame.Category + sLineBreak
        + 'Spielzeit: ' + CurrentGame.GameTime + ' ' + CurrentGame.Status + sLineBreak
        + 'Gegner: ' + CurrentGame.Opponent1 + IfThen(CurrentGame.RedCards1 <> '', ' (' + CurrentGame.RedCards1 + ' rote Karte(n))', '')
          + ' gegen ' + CurrentGame.Opponent2 + IfThen(CurrentGame.RedCards2 <> '', ' (' + CurrentGame.RedCards2 + ' rote Karte(n))', '') + sLineBreak
        + 'URL: ' + CurrentGame.URL + sLineBreak
        + 'Ergebnis: ' + CurrentGame.MatchResult1 + ' : ' + CurrentGame.MatchResult2 + ' ' + CurrentGame.MatchResultInfo + sLineBreak + sLineBreak);
    end;
  finally
    Games.Free;
  end;
end;
Sebastian Jänicke
AppCentral
  Mit Zitat antworten Zitat