- CSS selector vs. XPath usage
| Target |
Matching Nodes |
CSS 3 |
XPath |
| All nodes |
~ |
* |
//* |
| Find a level, level two, level three header node |
,, |
h1,h2,h3 |
//h1,//h2,//h3 |
| All P-Nodes |
<p> |
p |
//p |
| All child nodes of the P node |
<p>All nodes under the label |
p > * |
//p/* |
| Find all Li tags that contain attr attributes |
<li attr="~"> |
li[attr] |
li[@attr] |
| Find all Li tags with attr values of value |
<li attr="value"> |
li[attr=value] |
//li[@attr=‘value‘] |
| Find all div nodes with an ID value of item |
<div id="item"> |
div#item |
//div[@id=‘item‘] |
| Find all tags that contain foo in the class value |
<* class="foo blahblah"> |
.foo |
//*[contains(@class,‘foo‘)] |
| First P-node |
<p>the first of many<p> |
p:first-child |
//p[1] |
| Nth node of P |
The nth of <p> the numerous<p> |
p:nth-child |
//p[n] |
| All p nodes that have child node A |
<p><a></p> |
CSS cannot be implemented |
//p[a] |
| Find text content is the p node of "Web scraping" |
<p>Web Scraping</p> |
CSS cannot be implemented |
//p[text()="Web Scraping"] |
R Language Crawler: A comparison of CSS methods and XPath methods (table Introduction)