【发布时间】:2018-03-15 13:53:34
【问题描述】:
我有一个看起来像这样(缩短)的 html 代码;
<div id="activities" class="ListItems">
<h2>Standards</h2>
<ul>
<li>
<a class="Title" href="http://www.google.com" >Guidelines on management</a>
<div class="Info">
<p>
text
</p>
<p class="Date">Status: Under development</p>
</div>
</li>
</ul>
</div>
<div class="DocList">
<h3>Reports</h3>
<p class="SupLink">+ <a href="http://www.google.com/test" >View More</a></p>
<ul>
<li class="pdf">
<a class="Title" href="document.pdf" target="_blank" >Document</a>
<span class="Size">
[1,542.3KB]
</span>
<div class="Info">
<p>
text <a href="http://www.google.com" >Read more</a>
</p>
<p class="Date">
14/03/2018
</p>
</div>
</li>
</ul>
</div>
我正在尝试使用以下代码选择 'a class="Title"' 下的 'href=' 中的值:
def sub_path02(url):
page = requests.get(url)
tree = html.fromstring(page.content)
url2 = []
for node in tree.xpath('//a[@class="Title"]'):
url2.append(node.get("href"))
return url2
但是我得到了两个返回,'div class="DocList"' 下的一个也返回了。
我正在尝试更改我的 xpath 表达式,以便我只能在节点内查看,但我无法让它工作。
有人可以帮助我了解如何在特定节点内“搜索”。我浏览了多个 xpath 文档,但似乎无法弄清楚。
【问题讨论】: