在 Scrapy 中为同一个 ArticleItem 使用多个 CSS 选择器 [英] Using multiple CSS selectors for the same ArticleItem in Scrapy

查看:25
本文介绍了在 Scrapy 中为同一个 ArticleItem 使用多个 CSS 选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取的网站布局不一致.我目前正在使用它,但它没有返回所有标题 -

The site I am scraping has an inconsistent layout. I'm currently using this but its not returning all the titles -

article['title'] = sel.css('p[class=title] ::text').extract()

我还需要用它来抓取跨度类 -

I need to use this to scrape span classes also -

article['title'] = sel.css('span[class=newstitle] ::text').extract()

有没有办法在一个 ArticleItem 中组合两个 css 选择器?

Is there a way to combine two css selectors in a single ArticleItem?

推荐答案

就像列表串联一样简单:

As simple as list concatenation:

article['title'] = response.css("p.title ::text").extract() + \
                   response.css("span.newstitle ::text").extract()

这篇关于在 Scrapy 中为同一个 ArticleItem 使用多个 CSS 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆