【发布时间】:2018-04-19 05:59:31
【问题描述】:
我正在尝试使用 CSS 选择器获取突出显示的 href 值,但目前没有成功。
我正在使用scrapy shell 并尝试了这个:
response.css('body > span > section') 但它返回[]
我还尝试response.css('div') 看看它是否可以抓取任何div 标签,但它仍然返回[]
使用chrome的devtool抓取返回的css选择器
#react-root > section > main > article > div > div._cmdpi > div:nth-child(1) > div:nth-child(2) > a
我对 chrome 提供的 css 使用了 response.css(),但它也返回了 []
但是,当我尝试时:
response.css('body, span, section, main, article, div, div, div')
我收到了这个:
[<Selector xpath='descendant-or-self::body | descendant-or-self::span | descendant-or-self::section | descendant-or-self::main | descendant-or-self::article | descendant-or-self::div | descendant-or-self::div | descendant-or-self::div' data='<body class="">\n \n <span id="r'>, <Selector xpath='descendant-or-self::body | descendant-or-self::span | descendant-or-self::section | descendant-or-self::main | descendant-or-self::article | descendant-or-self::div | descendant-or-self::div | descendant-or-self::div' data='<span id="react-root"></span>'>]
我很困惑为什么某些 css 选择器有效而其他选择器不有效。比如div 和body, span, section, main, article, div, div, div
【问题讨论】:
-
可以用bs4吗?
select_one('a[href*=taken-by]')
标签: python html css css-selectors scrapy