【问题标题】:How to get the content or title of a wikipedia page using erlang?如何使用 erlang 获取维基百科页面的内容或标题?
【发布时间】:2018-01-05 16:03:03
【问题描述】:
-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]

【问题讨论】:

  • 哪一行出现nomatch 错误?您可以在问题中包含堆栈跟踪吗?
  • 另外,该页面是 xml,所以我建议使用 erlang.org/doc/apps/xmerl/xmerl_ug.html 来解析 XML 并提取您想要的内容。
  • 输出显示不匹配。@Stratus3D
  • 嗯,好的,所以io:format/2 调用正在打印nomatch,这意味着这是T 的值。这意味着 re:run/3 调用没有找到任何与您的正则表达式匹配的内容。
  • 这是有道理的,因为您的正则表达式除了字母和数字外不允许任何内容,但 XML 将包含许多其他字符。那个正则表达式应该在做什么?

标签: regex erlang wikipedia erlang-shell erlangweb


【解决方案1】:

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

-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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多