首页 文章

使用xpath获取属性

提问于
浏览
-1

给定HTML结构如下:

<dd itemprop="actors">
    <span itemscope="" itemtype="http://schema.org/Person">
        <a itemprop="name">Yumi Kazama</a>,                 </span>

<span itemscope="" itemtype="http://schema.org/Person">
    <a itemprop="name">Yuna Mizumoto</a>,               </span>

<span itemscope="" itemtype="http://schema.org/Person">
    <a itemprop="name">Rei Aoki</a>,                        </span>
</dd>

如何为所有 itemprop="name" 元素获取 a/text() 的所有值?

网址:

//*[@itemprop='actors']//*[@itemprop='name']/text()

只得到第一个 a/text .

1 回答

  • 1

    假设您的html文件是 test.html ,以下内容应该有效:

    from lxml import html
    
    with open(r'E:/backup/GoogleDrive/py/scrapy/test.html', "r") as f:
        page = f.read()
    tree = html.fromstring(page)
    names = tree.xpath("//a[@itemprop='name']//text()")
    print names
    

相关问题