【问题标题】:How do I scrape an item that has same <span class> as other items?如何抓取与其他项目具有相同 <span class> 的项目?
【发布时间】:2018-05-26 18:04:41
【问题描述】:

所以我遇到了这个反复出现的问题,我想在网页上抓取一个项目,但事实证明这个特定项目与其他项目具有相同的 span 类。如何将蜘蛛引导到正确的span class?

请参阅下面的示例。假设我想刮“105 平方米”。有什么想法吗?

【问题讨论】:

  • 你必须弄清楚你想要抓取的项目的一些不变属性。如果类名不是唯一的,则使用其他名称,例如列表中的固定索引(假设您要抓取的页面之间没有变化)。

标签: python html python-3.x scrapy scrapy-spider


【解决方案1】:

您可以使用包含li 在其兄弟中排名第三的信息,并使用该信息选择span:

ul.card-property__features > li:nth-child(3) > span.label__property

参考:nth-child selector

【讨论】:

  • 我试图将它合并到我的代码行中,但它不起作用。
  • for l in response.css('div.ev-teaser-content'): yield {'area': l.css('ul.card-property__features &gt; li:nth-child(3) &gt; span.label__property::text').extract()}
  • @fg42 我不确定您使用的库是否支持nth-child。您可以尝试逐步缩小选择器的范围吗?例如,这是否有效ul.card-property__features &gt; li:nth-child(3)?
猜你喜欢
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
相关资源
最近更新 更多