【问题标题】:Scrapy response.css - two tags without distinct identifiersScrapy response.css - 两个没有不同标识符的标签
【发布时间】:2020-05-02 19:50:33
【问题描述】:

我只是一个scrapy的初学者面临一些问题:

<tr>
<td rowspan="2" style="vertical-align: top; width: 20%;">
1.&nbsp;c4<br>

<script type="text/javascript">
...
<\script>

</td>
<td style="vertical-align: top;">The English Defense, here I give up the centre to Black as a target for attack.</td>
</tr>

如果我想同时获取"c4" 文本和"The English Defense, here I give up the centre to Black as a target for attack.",可以使用response.css('tr td::text').extract()

但是如果我只想要第二个&lt;td&gt; 标签的文本,我该怎么办,因为&lt;td&gt; 标签没有idclass 或其他任何东西? In this link,我没有找到使用stylerowspan的解决方案...

【问题讨论】:

    标签: css python-3.x scrapy web-crawler


    【解决方案1】:

    您可以使用第 n 个子选择器。在您的具体情况下,这将是:
    response.css("td:nth-child(2)::text").extract()

    【讨论】:

    • 谢谢!不知道这个语法。或者,我发现 xpath 也可以是一个选项!类似response.xpath('//tr/td[1]/text()').extract()
    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 2017-10-12
    • 2015-05-27
    • 1970-01-01
    • 2013-08-08
    相关资源
    最近更新 更多