0
-module(wikipedia).
-export([main/0]).
-define(Url, "http://en.wikipedia.org/w/api.php?format=xml&action=parse&prop=sections&page=Chicago").
-define(Match, "^[A-Za-z]+[A-Za-z0-9]*$").

main() ->
    inets:start(),
    %% Start ssl application
  ssl:start(),
    {ok, {_Status, _Header, Body}} = httpc:request(?Url),
    T = re:run(Body, ?Match, [{capture, all_but_first, binary}]),
    io:format("~s~n",[T]).

我想使用正则表达式匹配将维基百科页面的内容存储在“T”中。然后我要去取标题。但是上面的代码说不匹配。我不知道如何使用 erlang 获取维基百科页面的标题。请帮忙。(我是erlang的新手)。[我想要类似的东西:https://stackoverflow.com/questions/13459598/how-to-get-titles-from-a-wikipedia-page]

4

1 回答 1

2

首先,我认为标题已经在您的 URL 中:“芝加哥”,如果这种情况只是模式匹配 URL 以获取标题。如果不是这种情况,我建议您应该使用像xmlerl这样的 XML 解析模块:

-module(parse_title).
-include_lib("xmerl/include/xmerl.hrl").

-export([main/0]).

main() ->
  inets:start(),
  ssl:start(),
  U =  "http://en.wikipedia.org/w/api.php?format=xml&action=parse&prop=sections&page=Chicago",
  {ok, {_, _, Body}} = httpc:request(U),
  {Xml,_} = xmerl_scan:string(Body),
  [Title|_] = [Value || #xmlAttribute{value = Value} <- xmerl_xpath:string("//api/parse/@title", Xml)],
  Title.
于 2017-08-02T05:47:49.390 回答