python - scrapy获取不完整的html

标签 python html scrapy

我是一名退休程序员,但刚接触 scrapy。实际上,这是我的第一个 Python 项目,所以我可能会做错任何事情。

我在 anaconda 下启动了 scrapy 并启动了一个 shell:

 scrapy shell "https://sailing-channels.com/by-subscribers"

看起来一切工作正常,我可以进行一些查询。

这是我的问题: 当我输入:

response.css('body').extract()

我得到:['<body><noscript>If you\'re seeing this message, that means <strong>JavaScript has been disabled on your browser</strong>, please <strong>enable JS</strong> to make this app work.</noscript><div id="app"></div><script src="//apis.google.com/js/platform.js" async></script><script>!function(e,a,n,t,g,c,i){e.GoogleAnalyticsObject="ga",e.ga=e.ga||function(){(e.ga.q=e.ga.q||[]).push(arguments)},e.ga.l=1*new Date,c=a.createElement(n),i=a.getElementsByTagName(n)[0],c.async=1,c.src="//www.google-analytics.com/analytics.js",i.parentNode.insertBefore(c,i)}(window,document,"script"),ga("create","UA-15981085-17","auto"),ga("require","linkid"),ga("set","anonymizeIp",!0),ga("send","pageview")</script><script type="application/ld+json">{\n\t\t\t"@context": "http://schema.org",\n\t\t\t"@type": "Organization",\n\t\t\t"name": "Sailing Channels"\n\t\t\t"url": "https://www.sailing-channels.com",\n\t\t\t"logo": "https://sailing-channels.com/img/banner.png",\n\t\t\t"sameAs" : [\n\t\t\t\t"https://www.facebook.com/sailingchannels",\n\t\t\t\t"https://twitter.com/sailchannels"\n\t\t\t]\n\t }</script><script type="text/javascript" src="https://cdn.sailing-channels.com/1.15.9/main.1dad65fcb7a507930e1f.js"></script></body>']

我的问题是我期望更多。当我在 chrome 上进行检查时,我在 <div id="app"></div> 中看到了更多/div 部分。

有人可以指出我做错了什么吗?我想抓取 channel 名称、订阅者数量和观看次数

谢谢

最佳答案

可以理解。这是因为它们在页面加载期间通过另一个脚本呈现数据。

在正常的scrapy设置下,不会出现动态页面加载内容。要抓取该数据,您可以使用selenium
selenium-with-scrapy-for-dynamic-page

作为替代方法,您可以使用 splash 来处理启用 JavaScript 的内容。
handling-javascript-in-scrapy-with-splash

关于python - scrapy获取不完整的html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338207/

相关文章:

python - 如何使用函数创建一个 numpy 数组?

python - 使用创建的每一行的频率从 numpy 中的矩阵中提取唯一行

javascript - 在css中使用url()时如何设置图片的宽高

python - 如何使用scrapy的XmlFeedSpider解析sitemap.xml文件?

python - “wait_window”方法有什么作用?

python - 设置步长极坐标图 matplotlib python

jquery - 使用 JQuery 仅显示/隐藏一个 DIV

html - 当鼠标悬停在 DIV 中时更改 H2 颜色

python - Scrapy Shell - 如何更改 USER_AGENT

xpath - 网络抓取-从bandcamp.com获取项目标题