这是一个棘手的场景:
#target ~ p {
background: green;
color: white;
}
<h1 id="target">Title</h1>
<span>Some description</span>
<p>Yes</p>
<p>Yes</p>
<h1>Another Title</h1>
<span>Some description</span>
<p>No</p>
<p>No</p>
xpath 是否允许选择 sibling 但停在某个点?我想选择两个 <p>
第一个下<h1>
但不是第二个下的<h1>
.修改 HTML 是不可能的,因为我正在做一些网络抓取,我正在寻找一种快速而肮脏的方法来从某个标题下的段落中提取数据:
paragraphs = target.select("~ p")
最佳答案
试试这个:
#target ~ p:not(:nth-last-of-type(-n+2)) {
background: green;
color: white;
}
<h1 id="target">Title</h1>
<span>Some description</span>
<p>Yes</p>
<p>Yes</p>
<h1>Another Title</h1>
<span>Some description</span>
<p>No</p>
<p>No</p>
或者
#target + span + p,
#target + span + p + p {
background: green;
color: white;
}
<h1 id="target">Title</h1>
<span>Some description</span>
<p>Yes</p>
<p>Yes</p>
<h1>Another Title</h1>
<span>Some description</span>
<p>No</p>
<p>No</p>
或者
#target ~ p:nth-of-type(1),
#target ~ p:nth-of-type(2) {
background: green;
color: white;
}
<h1 id="target">Title</h1>
<span>Some description</span>
<p>Yes</p>
<p>Yes</p>
<h1>Another Title</h1>
<span>Some description</span>
<p>No</p>
<p>No</p>
关于python - 选择 sibling ,但中间没有任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829055/