0

I have a html file that has various html tags in it. This html also has a bunch of tables in it. I am processing this file using python. How do I find out what the size (length x width in pixels) when it is rendered by a browser (preferably chrome or firefox)?

I am essentially looking for the information when you do "inspect element" on a browser, and you are able to see the size of the various elements. I want to access this size in my python code.

I am using lxml to parse my html and can use selenium if needed.

edit: added #node.js incase I can use it to spit out the size of all the tables in a shell script and I can grab it in python.

4

2 に答える 2

1

Selenium WebDriverを使用して、Python コードが実行されているコンピューターにインストールされている実際のブラウザーで HTML ファイルを開くことができます。

Selenium WebDriver API を使用して、レンダリングされたテーブルの高さを調べる方法はわかりませんがvalue_of_css_propertyメソッドでそれができる可能性があります。

于 2013-04-15T10:57:08.567 に答える
0

シェルスクリプトを呼び出すことができ、Node.js を使用できる場合は、ヘッドレスWebKit ポートであるPhantomJSをインストールして使用することもできると思います。(つまり、ウィンドウを必要としない、実際の善良な WebKit レンダラーです。) これにより、Javascript と使い慣れた Web ライブラリを使用してドキュメントを操作できるようになります。例として、次の例では、スタック オーバーフロー サイトの左上に向かってロゴ要素の幅を取得します。

page = require('webpage').create(); // create a new "browser"

page.open('http://stackoverflow.com/', function() {
  // callback when loading completes
  var logoWidth = page.evaluate(function() {
    // This runs in the rendered page and uses the version of jQuery that SO loads.
    return $('#hlogo').width();
  });

  console.log(logoWidth); // prints 250, the same as Chrome.

  phantom.exit(); // for some reason you need to exit manually
});

PhantomJS のドキュメントでは、PhantomJS でできることとその方法について詳しく説明しています。

ただし、ページは CSS とスクリプトを取得する必要があり、通常はブラウザーが行うすべてのことを行う必要があるため、ページの読み込みに時間がかかるという注意点があります。PhantomJS がキャッシングを行うかどうか、またどのようにキャッシングを行うかはわかりません。もしそうなら、同じサイトの複数のスクレイピングに対して同じプロセスを再利用するのが理にかなっているかもしれません。

于 2013-04-16T23:48:58.117 に答える