解析从mochiweb_html获得的结果

use*_*836 2 erlang mochiweb

我想从一个html文件解析一些内容(没有xml).

目前,我使用mochiweb_html检索要解析的结构:

1> inets:start().
2> {ok, {Status, Headers, Body}} = httpc:request("http://www.google.com").
3> {String, Attributes, Other} = mochiweb_html:parse(Body).
Run Code Online (Sandbox Code Playgroud)

结果如下:

{<<"html">>,
 [{<<"itemscope">>,<<"itemscope">>},
  {<<"itemtype">>,<<"http://schema.org/WebPage">>}],
 [{<<"head">>,[],
   [{<<"meta">>,
     [{<<"itemprop">>,<<"image">>},
      {<<"content">>,<<"/images/google_favicon_128.png">>}],
     []},
    {<<"title">>,[],[<<"Google">>]},
....
Run Code Online (Sandbox Code Playgroud)

从mochiweb_http获取的结构中检索具有特定类(例如<span id="footer">)的特定标记的网页中所有元素的最佳方法是什么?

leg*_*cia 6

你可以使用mochiweb_xpath:

> mochiweb_xpath:execute("//span[@id='footer']",
    mochiweb_html:parse(
      "<html><body><span>not this one</span><span id='footer'>but this one</span></body></html>")).
[{<<"span">>,
  [{<<"id">>,<<"footer">>}],
  [<<"but this one">>]}]
Run Code Online (Sandbox Code Playgroud)