从具有样式属性的父标记分析子标记时出现Xpath问题

2024-04-27 04:16:40 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是html内容的片段:

<div class="post-inner wow bounceInUp animated" data-wow-offset='80' data-wow-delay="0s" data-wow-duration="0.8s">
   <a href="https://url.com/hello/" class="post-link"></a>
   <div class="post-pic lazyload" data-bg="https://url.com/wp-content/uploads/2019/01/opioid-300x200.jpg" *style="background-image: url('');" * /></div>
   <div class="tags-wrapper">
      <a href="/tag/hello-world">Hello World</a>
      <a href="/tag/noob">Noob</a>
   </div>
   <h3>
      <a href="https://url.com/hello/">
      My First Title-Hello</a>
   </h3>
</div>

我试图提取标题和h3内的链接。 我要做的是:

>>> from lxml.html import fromstring
>>> content = """
<div class="post-inner wow bounceInUp animated" data-wow-offset='80' data-wow-delay="0s" data-wow-duration="0.8s">
...    <a href="https://url.com/hello/" class="post-link"></a>
...    <div class="post-pic lazyload" data-bg="https://url.com/wp-content/uploads/2019/01/opioid-300x200.jpg" *style="background-image: url('');" * /></div
>
...    <div class="tags-wrapper">
...       <a href="/tag/hello-world">Hello World</a>
...       <a href="/tag/noob">Noob</a>
...    </div>
...    <h3>
...       <a href="https://url.com/hello/">
...       My First Title-Hello</a>
...    </h3>
... </div>"""
>>> html_response = fromstring(content)
>>> main_tag = html_response.xpath('//div[@class="post-inner wow bounceInUp animated"]')
>>> main_tag
[<Element div at 0x106b347e0>]
>>> main_tag[0].xpath('div')
[<Element div at 0x106b34788>]
>>> main_tag[0].xpath('a')
[<Element a at 0x106b34838>]
>>> main_tag[0].xpath('a/@href')
['https://url.com/hello/']
>>> main_tag[0].xpath('h3/a')
[]
>>> main_tag[0].xpath('h3')
[]
>>> 

我无法通过这里的h3标签。排除故障时,如果我 *style="background-image: url('');" * /

我可以提取标签

有人能帮我吗


Tags: httpsdivcomurlhellodatamainhtml
1条回答
网友
1楼 · 发布于 2024-04-27 04:16:40

您正在捕获的div在第3行的末尾结束(注意该行的第一个div/>结尾)。因此,要捕获的h3元素不在该div

相关问题 更多 >