Autor Beitrag
reg107
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 36



BeitragVerfasst: Do 17.03.05 14:54 
Hallo,
ich habe einen HTML-Quelltext und möchte nun alle Links herausfinden, also alles was immer zwischen href=" und " steht. Ich hab gelesen, dass regex nicht so schnell ist wie die Stringfunktionen in Delphi. Welche String-Funktion(en) muss ich da verwenden?

danke im voraus
ScorpionKing
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1150

Win XP

BeitragVerfasst: Do 17.03.05 14:57 
also:

ausblenden Suchen von Positionen im String
1:
pos();					


suche mal nach Suche in: Delphi-Forum, Delphi-Library POS STRING

MfG, ScorpionKing!

_________________
Aus dem Urlaub zurück!
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 17.03.05 14:58 
Pos und Copy.
Siluro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 74

Win XP Prof., Win XP64 Prof., Win 7 Prof.
Delphi 7 Ent., Delphi XE, Delphi XE5
BeitragVerfasst: Do 17.03.05 15:10 
Ich würde auch 'pos' und 'copy' benutzen, eventuell noch 'delete' um den bereits abgearbeiten Teil des Strings zu löschen.

MfG

Björn
reg107 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 36



BeitragVerfasst: Do 17.03.05 15:20 
mit pos finde ich dann die position des href=" aber wie finde ich das ende?
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 17.03.05 15:23 
Dafür brauchst du dann das delete, damit du das erste " rausschmeißen kannst. Dann liefert dir Pos das nächste ".
ScorpionKing
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1150

Win XP

BeitragVerfasst: Do 17.03.05 15:23 
du suchst einfach nach dem zweiten "!

MfG, ScorpionKing!

_________________
Aus dem Urlaub zurück!
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: Do 17.03.05 15:24 
[ot]Du arbeitest nicht zufällig gerade an der BWINF aufgabe, oder?[/ot]

_________________
Linux und OpenSource rulez!
Siluro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 74

Win XP Prof., Win XP64 Prof., Win 7 Prof.
Delphi 7 Ent., Delphi XE, Delphi XE5
BeitragVerfasst: Do 17.03.05 15:31 
Also ich würde das so machen

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);
var a: integer;
    s: string;
begin
  s:=edit1.Text;
  a:=pos('href="', s)+5;
  delete(s, 1, a);
  a:=pos('"', s);
  edit1.Text:=copy(s, 1, a-1);
end;


MfG

Björn
reg107 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 36



BeitragVerfasst: Do 17.03.05 15:39 
[ot]was ist die bwinf aufgabe[/ot]

@björn, dann hätte ich den ersten link. in dem string sind aber ganz viele und es können auch normale " drin sein, die nichts mit dem link zu tun haben. deswegen brauche ich ja auch alle sachen die zwischen einem href=" und einem " stehen.(wobei die "" auch wegfallen könnten.)

=>> eigentlich brauche ich immer die sachen, die hinter einem href= kommen. die " kann ich ja notfalls rausnehmen.
wie finde ich jetzt heraus, wie bekomm ich jetzt den link daraus?
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: Do 17.03.05 15:41 
@Siluro
das Problem dabei ist, dass auch folgendes zugelassen ist: href = "..." und diesen Fall würdest du mit deinem Code nicht einschließen (die Anzahl der Leerzeichen ist beliebig).

_________________
Linux und OpenSource rulez!
Siluro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 74

Win XP Prof., Win XP64 Prof., Win 7 Prof.
Delphi 7 Ent., Delphi XE, Delphi XE5
BeitragVerfasst: Do 17.03.05 15:48 
@sourcehunter: Da hast du natürlich recht, daran hab ich nicht gedacht.
Wonach würdest du denn suchen, nur nach ", oder erst nach href und dann nach "?

@reg107: Wenn du den ersten Link damit findest, funktionierts ja. Dann packst du das einfach in ne Schleife.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
while (pos(href, s)>0) do
begin

end;


MfG

Björn
reg107 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 36



BeitragVerfasst: Do 17.03.05 16:25 
gut, dann hab ich die positionen aller hrefs. dann brauch ich nur noch das was bis zum nächsten leerzeichen (oder ">") kommt
Siluro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 74

Win XP Prof., Win XP64 Prof., Win 7 Prof.
Delphi 7 Ent., Delphi XE, Delphi XE5
BeitragVerfasst: Do 17.03.05 16:41 
Wenn du, wie du gesagt hast, hiermit

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);   
var a: integer;   
    s: string;   
begin   
  s:=edit1.Text;   
  a:=pos('href="', s)+5;   
  delete(s, 1, a);   
  a:=pos('"', s);   
  edit1.Text:=copy(s, 1, a-1);   
end;


den ersten Link gefunden hast, dann müsstest du so

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button1Click(Sender: TObject);   
var a: integer;   
    s: string;      
begin  
  s:=edit1.Text;
  while (pos(href, s)>0) do   
    begin   
      a:=pos('href="', s)+5;   
      delete(s, 1, a);   
      a:=pos('"', s);   
      edit1.Text:=edit1.text+copy(s, 1, a-1);
    end;   
end;


eigentlich alle finden.

Nur mal so nebenbei, sehen bei dir alle Links gleich aus? Wenn ja kannst du ja mal einen posten.
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: Do 17.03.05 20:57 
@reg107: BWINF Aufgaben sind die Aufgaben des Bundeswettbewerbs Informatik. Und da geht es diesmal in einer auch um das Parsen von Websites nach Links.

Aber im Prinzip solltest du dich an das halten, was Siluro vorgeschlagen hat.

_________________
Linux und OpenSource rulez!
reg107 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 36



BeitragVerfasst: Do 17.03.05 21:05 
ich hab es geschafft. soll ich es posten, oder wäre das dann unfair?
die funktion holt alle links raus(egal ob href="", <a href=www.google.de>; oder <a href=www.google.de title="bla">) und macht aus internen links die volle url (moep.html --> www.google.de/dir/moep.html).

ansonsten bin ich auch interessier, wie die profis das gelöst haben.
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: Do 17.03.05 21:08 
Mich würde das schonmal interessieren, wie du das gelöst hast und ich denke dir wird keiner den Kopf abreißen, wenn du das hier postest. Die Aufgabe ist ja auch wesentlich umfangreicher.

_________________
Linux und OpenSource rulez!
reg107 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 36



BeitragVerfasst: Do 17.03.05 21:21 
es ist noch nicht ganz fertig siehe: www.delphi-forum.de/...ebergeben_38532.html
funktioniert nicht!
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
{--------------------------
       Get Base URL
--------------------------}

function get_base_url(url:string):string;
var a, d:string;
    b:integer;
  function reverse(a:string):string;
  var
    b:string;
    i:integer;
  begin
    b:=a;
    for i:=1 to length(a) do
      b[length(a)-i+1]:=a[i];
    result:=b;
  end;
begin
    // url umdrehen
    a:=reverse(url);
    // alles bis zum ersten / löschen (von url das letzte /)
    b:=pos('/', a);
    delete(a, 1, b);
    // wieder umdrehen ;-)
    d:=reverse(a)+'/';
    // nicht das wir schon eine base url hatten und jetzt ein http:/ daraus machen
    if d='http://' then d:=url;
    result:=d;
end;

{----------------------
       Get Links
----------------------}

function tform1.get_links(source:string):tstringlist;
var a,b,i:integer;
  links:tstringlist;
begin
links:=tstringlist.create;
try
  // bis keine links mehr da sind
  while(pos('href', source)>0) do
  begin
    a:=pos('href=', source)+4;
    // bis nach href loeschen
    delete(source, 1, a);
    // wenn link in ", " loeschen und end-" suchen
    if(source[1]='"')then
    begin
      delete(source, 1, 1);
      a:=pos('"', source);
    end
    // sonst pruefen ob link bei leerzeichen oder > aufhoert und zuweisen
    else
    begin
      a:=pos(' ', source);
      b:=pos('>', source);
      if(a>b)then a:=b;
    end;
    // link tstring liste hinzufuegen
    links.add(get_base_url(copy(source, 1, a-1)));
    // leere strings aus liste loeschen
    for i:=pred(links.count) downto 0 do
    begin
      if links[i]='' then links.delete(i);
    end;
  end;
  result:=links;
finally
  links.free; // das darf nicht sein..
end;
end;

das mit der base url funktioniert wohl auch erst morgen.. ;-)