THTMLDocument and GetElementsByClassName
lazarus / 21-General
----------------------------------------------------------------------
Slawek · 2023-04-12 08:45
Hi,
I'm looking at the DOM and DOM_HTML units, but I don't see a
GetElementsByClassName function anywhere, or anything like that that
would search the html tree for class names. Are there any other units
or packages that include GetElementsByClassName? Do I have to write
such a function myself?
Edit:
I did something like below. But I don't know if I'm reinventing the
wheel.
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i type {
TByClassNameNodeList } TByClassNameNodeList = class(TDOMNodeList)
protected FFilter: DOMString; function NodeFilter(aNode:
TDOMNode): TFilterResult; override; public constructor
Create(aNode: TDOMNode; const aFilter: DOMString); end; {
TExtHTMLDocument } TExtHTMLDocument = class(THTMLDocument)
private public function GetElementsByClassName(const ElementName:
DOMString): TDOMNodeList; constructor Create; destructor
Destroy; override; end; implementation { TByClassNameNodeList
} function TByClassNameNodeList.NodeFilter(aNode: TDOMNode):
TFilterResult;begin if (aNode.NodeType = ELEMENT_NODE) and
(TDOMElement(aNode)['class'] = FFilter) then Result := frTrue
else Result := frFalse;end; constructor
TByClassNameNodeList.Create(aNode: TDOMNode; const aFilter:
DOMString);begin inherited Create(aNode); FFilter := aFilter;end; {
TExtHTMLDocument } function
TExtHTMLDocument.GetElementsByClassName(const ElementName: DOMString):
TDOMNodeList;begin Result := TByClassNameNodeList.Create(Self,
ElementName);end; constructor TExtHTMLDocument.Create;begin inherited
Create;end; destructor TExtHTMLDocument.Destroy;begin inherited
Destroy;end;
----------------------------------------------------------------------
Jurassic Pork · 2023-04-12 12:20
Hello,
you can try to use the XPath unit to retrieve class element.
Example to retrieve the text of a tag div with classname equal to post
:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
implementationuses fphttpclient, opensslsockets, DOM, DOM_HTML,
SAX_HTML, XPath;{$R *.lfm}{ TForm1 }procedure
TForm1.Button1Click(Sender: TObject);var myStr : string; Doc:
THTMLDocument; XPathResult: TXPathVariable; cli: TFPHTTPClient;
sRetText: string;begin cli :=
TFPHTTPClient.Create(nil); cli.AddHeader('User-Agent','Mozilla/5.0
(X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0'); try try
cli.AllowRedirect := true; myStr := cli.Get('https://forum.lazarus.
freepascal.org/index.php/topic,63019.msg476904/');
readhtmlfile(doc,tstringstream.create(mystr)); XPathResult :=
EvaluateXPathExpression('//div[@class="post"]', Doc.DocumentElement);
sRetText := String(XPathResult.AsText); Memo1.Append(sRetText);
except Memo1.Append('oops'); end; finally XPathResult.Free;
Doc.Free; cli.Free; end;end;
Friendly, J.P
----------------------------------------------------------------------
Slawek · 2023-04-12 14:12
Thank you.
I tried your example and it works
I was wondering where I can find documentation on using
AExpressionString in EvaluateXPathExpression.
But I think I found it here https://lazarus-ccr.sourceforge.io/docs/la
zutils/laz2_xpath/evaluatexpathexpression.html
and here https://www.w3.org/TR/1999/REC-xpath-19991116/
----------------------------------------------------------------------
dsiders · 2023-04-12 19:57
Quote from: Slawek on April 12, 2023, 04:12:24 pmThank you.
I tried your example and it works
I was wondering where I can find documentation on using
AExpressionString in EvaluateXPathExpression.
But I think I found it here https://lazarus-ccr.sourceforge.io/docs/la
zutils/laz2_xpath/evaluatexpathexpression.html
and here https://www.w3.org/TR/1999/REC-xpath-19991116/
In the original, the attribute value is a list. This shouold work
Code: XML [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
//*[contains(@class, "lang-en")]
----------------------------------------------------------------------
Slawek · 2023-04-12 23:06
Quote from: dsiders on April 12, 2023, 09:57:59 pmIn the original, the
attribute value is a list. This shouold work
Code: XML [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
//*[contains(@class, "lang-en")]I edited the post and deleted the
question.
The problem was that the class "lang-pl" was added by javascrypt
(that's why I saw it in the browser) and there was no such class in
the saved html file, therefore EvaluateXPathExpression could not find
it.
Also everything is fine Thanks
----------------------------------------------------------------------
Jurassic Pork · 2023-04-12 23:18
Another example to get the 10 most recent topics in the lazarus forum
:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i procedure
TForm1.Button1Click(Sender: TObject);var myStr : string; Doc:
THTMLDocument; XPathResult: TXPathVariable; elem: Pointer;
cli: TFPHTTPClient; i: integer;begin cli :=
TFPHTTPClient.Create(nil); cli.AddHeader('User-Agent','Mozilla/5.0
(X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0'); try try
cli.AllowRedirect := true; myStr :=
cli.Get('https://forum.lazarus.freepascal.org/index.php');
readhtmlfile(doc,tstringstream.create(mystr)); XPathResult :=
EvaluateXPathExpression('//ul[@class="recent_topics"]/li/a',
Doc.DocumentElement); i := 1; For elem in XPathResult.AsNodeSet
do Begin Memo1.Append(IntToStr(i) + ' - ' +
String(TDomElement(elem).GetAttribute('title'))); Inc(i);
end; except Memo1.Append('oops'); end; finally
XPathResult.Free; Doc.Free; cli.Free; end;end;
----------------------------------------------------------------------
Slawek · 2023-04-13 07:16
Thanks