python-3.x - xpath:如何提取“强”标签中的文本?
问题描述
我正在使用scrapy,需要使用xpath 选择器提取“灰色/灰色”。这是html片段:
<div class="Vehicle-Overview">
<div class="Txt-YMM">
2006 GMC Sierra 1500
</div>
<div class="Txt-Price">
Price : $8,499
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0"
class="Table-Specs">
<tr>
<td>
<strong>2006 GMC Sierra 1500 Crew Cab 143.5 WB 4WD
SLE</strong>
<strong class="text-right t-none"></strong>
</td>
</tr>
<tr>
<td>
<strong>Gray / Gray</strong><br />
<strong>209,123
Miles
/ VIN: XXXXXXXXXX
</td>
</tr>
</table>
我一直试图在“强”标签中提取“灰色/灰色”。任何帮助表示赞赏。
解决方案
这个 XPath 可以在 Scrapy 和 Google/Firefox 开发者控制台中工作:
//div[@class='Vehicle-Overview']/table[@class='Table-Specs']//tr[2]/td[1]/strong[1]/text()
您可以在您的蜘蛛中使用此代码:
color = response.xpath("//div[@class='Vehicle-Overview']/table[@class='Table-Specs']//tr[2]/td[1]/strong[1]/text()").extract_first()
推荐阅读
- javascript - React:没有得到加载骨架的语法
- sql - postgres sql查找具有状态和日期序列的记录
- reactjs - 如何将高阶组件作为子组件附加
- python - 将浮点数转换回时间戳
- visual-studio - 如果您在 Git 存储库中有一个没有上游的分支,并且有多个遥控器,那么 Visual Studio 如何在您推送时选择使用哪个遥控器?
- python - 如何在开头没有空格的情况下打印此列表?
- node.js - 使用数组过滤器 (MongoDB) 时无法使用聚合运算符 $add 更新日期
- reactjs - 使用 store 中的数据时如何避免重新渲染功能组件
- cython - 为什么我不能在 Jupyterlab 的 Cython 中使用 int 参数?
- javascript - 单击时滚动到不滚动到正确的列表项