title: Extract more than one type of element whilst preserving order using rvest (or similar) in R?
body: <p>I'm attempting to extract elements matching 2 different types in an HTML document, whilst preserving the order.</p>
  <p>It's straight forward to extract each element type separately (see example), but I cannot work out how to extract them both in one go and preserve the order in which they appear in the web page.</p>
  <h3>Minimal example</h3>
  <p>Here's some dummy HTML</p>
  <pre><code>dummy_html &lt;- &quot;&lt;p&gt;hi there&lt;/p&gt;
  &lt;p&gt;2nd para&lt;/p&gt;
  &lt;div&gt;unwanted stuff&lt;/div&gt;
  &lt;span&gt;something new&lt;/span&gt;
  &lt;p&gt;3rd para&lt;/p&gt;
  &lt;span&gt;extra stuff&lt;/span&gt;
  &lt;div&gt;more unwanted stuff&lt;/div&gt;
  &lt;p&gt;4th para&lt;/p&gt;&quot;
  </code></pre>
  <p>Suppose we wish to extract all the <code>p</code> elements and all the <code>span</code> elements (and maintain the order in which they appear)</p>
  <pre><code># p elements on their own
  library(rvest)
  dummy_html %&gt;% read_html %&gt;% html_nodes(&quot;p&quot;)
  
  {xml_nodeset (4)}
  [1] &lt;p&gt;hi there&lt;/p&gt;
  [2] &lt;p&gt;2nd para&lt;/p&gt;
  [3] &lt;p&gt;3rd para&lt;/p&gt;
  [4] &lt;p&gt;4th para&lt;/p&gt;
  
  # span elements on their own
  dummy_html %&gt;% read_html %&gt;% html_nodes(&quot;span&quot;)
  {xml_nodeset (2)}
  [1] &lt;span&gt;something new&lt;/span&gt;
  [2] &lt;span&gt;extra stuff&lt;/span&gt;
  </code></pre>
  <p>But how can we extract all of <strong>either</strong> element? i.e. all the p elements and all the span elements <em><strong>together</strong></em> so that the desired output is:</p>
  <pre class="lang-r prettyprint-override"><code>{xml_nodeset (6)}
  [1] &lt;p&gt;hi there&lt;/p&gt;
  [2] &lt;p&gt;2nd para&lt;/p&gt;
  [3] &lt;span&gt;something new&lt;/span&gt;
  [4] &lt;p&gt;3rd para&lt;/p&gt;
  [5] &lt;span&gt;extra stuff&lt;/span&gt;
  [6] &lt;p&gt;4th para&lt;/p&gt;
  
  </code></pre>
  <p>Note the preservation of the order (i.e. <code>p</code> and <code>span</code> interspliced)</p>
  <h3>What I've tried so far</h3>
  <p>I tried the obvious <code>dummy_html %&gt;% read_html %&gt;% html_nodes(&quot;span|p&quot;)</code> but it throws an error.</p>