What causes the following unexpected behavior?

In[1]:= ImportString["a<span>b</span>c", "HTML"]
Out[1]= "a b c"

In[2]:= ImportString["a<foo>b</foo>c", "HTML"]
Out[2]= "a bc"

In both cases, I would expect the returned string to be "abc".

What is the best way to work around it?


3 Answers 3


I don't know what causes the issue (which looks like a bug to me), but I generally find the best way to deal with exerting control over Import not doing quit the right thing with HTML is to use the "XMLObject" element and use ordinary Mathematica functions on it, like so:

In[1]:= ImportString@
          ImportString["a<span>b</span>c", {"HTML", "XMLObject"}] /. 
             Except["html" | "body" | "p" | "li" | "table" | "tr" | "td" | 
               "br" | "ul" | "li"], {___}, {stuff___}] :> stuff, "XML"]
Out[1]= "abc"

It's kinda cumbersome to round-trip like that, but on the upside it's very flexible.

EDITed to strip out a wider range of tags.

  • $\begingroup$ Unfortunately, it doesn't for for other known tags (e.g. <i>...</i>) $\endgroup$
    – ybeltukov
    Commented Nov 14, 2015 at 23:10
  • $\begingroup$ @ybeltukov yeah, that's an issue; it wouldn't be so hard to fix, but I don't know which tags are (or, perhaps more usefully, aren't) supposed to work that way. Google University time, I guess. $\endgroup$
    – Pillsy
    Commented Nov 14, 2015 at 23:19

jSoupLink can be used as a workaround, like this:

<< jSoupLink`
html2text[html_] := First@jSoupLink`ParseHTMLFragment[
   "<div id=\"container\">" <> html <> "</div>", "#container", "text"

(* Out: abc *)

(* Out: abc *)

One can import raw text and use this example to remove HTML tags

StringReplace[  #, "<" ~~ Except[">"] .. ~~ ">" -> ""] &@
 ImportString["a<bar>b</bar>c", {"HTML", "Source"}]
(* "abc" *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.