gpt4 book ai didi

python - 使用css选择器使用scrapy在Reactjs页面上抓取嵌套标签

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:47 24 4
gpt4 key购买 nike

我正在尝试使用 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 选择器可以工作而其他选择器不能工作。就像 divbody, span, section, main, article, div, div, div

enter image description here

最佳答案

我认为这是因为您在浏览器中看到的 html 代码可能是在客户端使用 javascript 生成的。我建议您检查用 scrapy 收到的 html(如果需要,您可以将 response.body 保存在文件中)或在 shell 中检查它。css 选择器中的 , 类似于 语句。该 href 所需的数据可能在 html 代码内的 json 中。

关于python - 使用css选择器使用scrapy在Reactjs页面上抓取嵌套标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47147679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com