xPath:如何从表格中获取'title'文本?



我正在使用xPath尝试从表的以下部分获取title文本:

    <td class="title" title="if you were in a job and then one day, the work..." data-id="3198695">
        <span id="thread_3198695" class="titleline threadbit">
            <span class="prefix">

            </span>
            <a id="thread_title_3198695" href="showthread.php?t=3198695">would this creep you out?</a>
            <span class="thread-pagenav">(Pgs:
                 <span><a href="showthread.php?t=3198695">1</a></span> <span><a href="showthread.php?t=3198695&amp;page=2">2</a></span> <span><a href="showthread.php?t=3198695&amp;page=3">3</a></span> <span><a href="showthread.php?t=3198695&amp;page=4">4</a></span>)</span>
        </span>
        <span class="byline">

                by
                <a href="member.php?u=1687137" data-id="3198695" class="username">
                    damoni
                </a>
        </span>
</td>

我想要的输出是"if you were in a job and then one day, the work..."

我一直在尝试Scrapy(python)中的各种表达式,以尝试获取title。它输出一个奇怪的文本,例如:'nn r r n nnr'

 response.xpath("//tr[3]/td[@class='title']/text()")

我知道以下部分是正确的,至少(我验证了它使用 Chrome 的开发人员工具定位了正确的表元素:

//tr[3]/td
# (This is the above snippet)

关于如何提取title的任何想法?

你需要:

response.xpath("//tr[3]/td[@class='title']/@title")

请注意,text()选择节点的文本内容,但@attribute属性的值。由于所需的文本存储在 title 属性中,因此您需要使用 @title .

最新更新