猿问

如何用 scrapy 抓取交互式图表?

scrapy用来抓取和抓取网页。

我对如何抓取此页面感兴趣。如您所见,有几个图表。但是当我查看源代码时,我没有找到点的值,即使我在<script>标签中搜索也是如此。

如何x以小时和y奇数的形式抓取每个图表?

Scrapy 版本:1.5.2


繁星点点滴滴
浏览 204回答 2
2回答

动漫人物

数据在您的网页上。script签入标签变量var cote_data_1,var cote_data_2等。它们应该在没有 JS 的情况下可用。

杨魅力

这是抓取第一个图表数据的代码:import scrapyimport astdef find_between(s, start, end):&nbsp; return (s.split(start))[1].split(end)[0]class CanalTurfSpider(scrapy.Spider):&nbsp; &nbsp; name = "CanalTurfSpider"&nbsp; &nbsp; start_urls = ['https://www.canalturf.com/cotes/2019-04-15/'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'maisons-laffitte/185850_prix-des-ecuries-du-chateau.html']&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def parse(self, response):&nbsp; &nbsp; &nbsp; &nbsp; data = response.xpath('//script').extract()[-1]&nbsp; &nbsp; &nbsp; &nbsp; chart1_data = find_between(data, "var cote_data_1 = ", ";")&nbsp; &nbsp; &nbsp; &nbsp; chart1_data = ast.literal_eval(chart1_data)&nbsp; &nbsp; &nbsp; &nbsp; yield {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "chart1_data": chart1_data&nbsp; &nbsp; &nbsp; &nbsp; }输出:{'chart1_data': [{'elapsed': '12:25', 'value': 9.3}, {'elapsed': '12:35', 'value': 9.7}, {'elapsed': '12:45', 'value': 10}, {'elapsed': '12:55', 'value': 10.1}, {'elapsed': '13:05', 'value': 10.6}, {'elapsed': '13:15', 'value': 10.6}, {'elapsed': '13:25', 'value': 11.2}, {'elapsed': '13:35', 'value': 11.3}, {'elapsed': '13:45', 'value': 13.1}, {'elapsed': '13:55', 'value': 14.7}, {'elapsed': '14:05', 'value': 18.8}, {'elapsed': '14:15', 'value': 18.8}]}您可以在最后一个脚本块中找到变量。
随时随地看视频慕课网APP

相关分类

Python
我要回答