4
$\begingroup$

I'm trying to parse some data out of the HTML for a form. Here's a simple one

CloudDeploy[
 FormFunction[
  {"Name" -> "String", "Email" -> "EmailAddress"},
  TemplateApply["Thanks ``! Your submission went through.", {#Name}] &
  ],
 "testForm",
 Permissions -> "Public"
 ]

It's easy enough to get the raw HTML body

body = URLRead[CloudObject["testForm"]]["Body"]

and I see a large <script> in the header + some stuff in the body.

But if I use Import that all goes away

ImportString[body, {"HTML", "XMLObject"}]

XMLObject["Document"][{XMLObject["Declaration"]["Version" -> "1.0", 
   "Standalone" -> "yes"]}, 
 XMLElement[
  "html", {"lang" -> "en", {"http://www.w3.org/2000/xmlns/", "xmlns"} -> 
    "http://www.w3.org/1999/xhtml"}, {XMLElement[
    "head", {}, {XMLElement["title", {}, {"
      Wolfram
    "}], XMLElement["meta", {"charset" -> "utf-8"}, {}], 
     ...,
     XMLElement[
      "script", {"defer" -> "defer", "type" -> "text/javascript", 
       "src" -> 
        "/dist/dyn/vendors~Dialogs~account~authLanding~dashboard~deployedView~\
directoryPlaceholderLanding~error401Permi~17e11331-4e693e624e6d42331857.js"}, \
{}], XMLElement[
      "script", {"defer" -> "defer", "type" -> "text/javascript", 
       "src" -> "/dist/dyn/formView-4db6f811488735b8a396.js"}, {}], 
     XMLElement["script", {}, {"
if(typeof LAZY_RESOURCES === 'undefined'){LAZY_RESOURCES={}}
"}]}]}], {}]

I tracked the issue down to System`Convert`HTMLImportDump`FixFile but I'm not sure how I should best patch this since the damage happens on the Java side (the function uses JLink)

$\endgroup$
2
  • $\begingroup$ Saved the file and opened it VSCode, the syntax coloring was not right (in Sublime it was ok), after formating the HTML code, the Import works fine. I could trace the problem to line 224 (starts with !function(e){function d(d) ...), If you delete it, the import will also work fine. $\endgroup$
    – Ben Izd
    Feb 2, 2022 at 21:57
  • $\begingroup$ @BenIzd nice to know, but unfortunately that's beyond my control. I found a workaround that I'll post in a minute $\endgroup$
    – b3m2a1
    Feb 2, 2022 at 22:14

1 Answer 1

1
$\begingroup$

Here is a quick workaround using "Python" with "beautifulsoup4", a third-party module to format the HTML text.

  1. First install "beautifulsoup4"/"bs4" (pypi)

  2. create a function to use the module

prettify = ExternalFunction["Python", "
from bs4 import BeautifulSoup as bs

def temp(text):
    return bs(text).prettify()
"]

apply it to strings:

ImportString[text, {"HTML", "XMLObject"}] // Depth
(* Out: 9 *)

ImportString[prettify @ text, {"HTML", "XMLObject"}] // Depth
(* Out: 27 *)
$\endgroup$

Your Answer

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

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