Webページからhtml要素の座標を抽出するWebクローラーを開発する予定です。「mshtml」アセンブリを使用すると、html 要素の座標を取得できることがわかりました。今、私はそれが可能かどうか、そしてどのようにWebページから必要な情報(html、css)だけを取得し、適切なmshtmlクラスを使用してすべてのhtml要素の正しい座標を取得するかを知りたいですか?
ありがとうございました!
Webページからhtml要素の座標を抽出するWebクローラーを開発する予定です。「mshtml」アセンブリを使用すると、html 要素の座標を取得できることがわかりました。今、私はそれが可能かどうか、そしてどのようにWebページから必要な情報(html、css)だけを取得し、適切なmshtmlクラスを使用してすべてのhtml要素の正しい座標を取得するかを知りたいですか?
ありがとうございました!
これらのc#関数を使用して、要素の位置を決定します。問題のHTML要素への参照を渡す必要があります。
public static int findPosX( mshtml.IHTMLElement obj )
{
int curleft = 0;
if (obj.offsetParent != null )
{
while (obj.offsetParent != null )
{
curleft += obj.offsetLeft;
obj = obj.offsetParent;
}
}
return curleft;
}
public static int findPosY( mshtml.IHTMLElement obj )
{
int curtop = 0;
if (obj.offsetParent != null )
{
while (obj.offsetParent != null )
{
curtop += obj.offsetTop;
obj = obj.offsetParent;
}
}
return curtop;
}
次のように、現在のドキュメントからHTML要素を取得します。
// start an instance of IE
public SHDocVw.InternetExplorerClass ie;
ie = new SHDocVw.InternetExplorerClass();
ie.Visible = true;
// Load a url
Object Flags = null, TargetFrameName = null, PostData = null, Headers = null;
ie.Navigate( url, ref Flags, ref TargetFrameName, ref PostData, ref Headers );
while( ie.Busy )
{
Thread.Sleep( 500 );
}
// get an element from the loaded document
mshtml.HTMLDocumentClass document = ((mshtml.HTMLDocumentClass)ie.Document);
document.getElementById("myelementsid");
私の選択した言語ではないため、C#でこれを行う方法はわかりませんが、Javascript、特にjQueryのoffSet() functionを使用して行うことができます。