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?


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.

| improve this answer | |
  • $\begingroup$ Unfortunately, it doesn't for for other known tags (e.g. <i>...</i>) $\endgroup$ – ybeltukov Nov 14 '15 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 Nov 14 '15 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 *)
| improve this answer | |

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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