我希望获得有关如何在Python中使用Scrapy提取数据的帮助,请参见以下页面
https://fincaraiz.com.co/apartamentos/arriendos?ubicacion=cali
我需要提取每个项目的链接,例如,当鼠标悬停在照片上时,第一个项目会显示一个详细链接:
https://fincaraiz.com.co/inmueble/apartamento-en-arriendo/florida-blanca/bogota/6738284
问题
这个页面动态加载内容,所以当我从Scrapy发出请求时,我得到的结果是HTML、CSS、JavaScript和一些东西。
但是,没有获得公寓本身的数据。
所以我不能应用XPath,因为响应不包含数据,而是动态加载的。
问题
如何在不使用Selenium、Scrapy Splash或其他外部库的情况下刮取它?
2条答案
按热度按时间ui7jx7zq1#
前面的答案需要手动收集每个不同url的
Json
,这是一个麻烦的工作。我找到了一个更简单的解决方案,它允许您上传多个start_urls
,并根据需要排列json
以获得所需的数据我已经集成了
scrapy_playwright
而不是scrapy_splash
,因为splash
将返回一个空白屏幕,并且无论出于什么原因都不会加载-无论我设置了多长时间的计时器。Playwright加载单个页面需要几秒钟的时间,但是Scrapy的速度很快,因为它是异步的,所以你可以快速地从页面中检索所有的json。
以下是脚本:
输出量:
b4lqfgs42#
当你在站点加载时检查network panel时,你可以找到它用来动态加载内容的api调用,然后你可以简单地通过复制curl请求和converting到python来复制api调用:
对于第二个URL(https://fincaraiz.com.co/apartamentos/arriendos/florida-blanca/zona-occidente/bogota?pagina=1):
data['hits']['hits']
中有11个项目