【发布时间】:2017-04-21 04:54:47
【问题描述】:
我是scrapy的新手,需要为数据挖掘项目抓取一些数据集。我需要刮掉“http://www.moneycontrol.com/india/stockpricequote/”。按照每个链接并提取数据。我已经编写了一个工作的scrapy爬虫来使用xpth和css获取数据。但是我在页面中遇到了这个元素,它使用javascript来填充一个选项卡式表。每个选项卡的 xpath 都相同。因此无法为单个选项卡提取数据 并从每个选项卡中获取数据库存增益百分比this is the tabbed element with gainpercentage in 5th row last column
我可以从 xpath 和 css 中抓取数据,但页面的一部分是从 javascript 中获取的。怎么能刮到这样的数据?我还需要每个选项卡中的数据 请告诉我一种方法,因为其他答案使用 json,我不熟悉它。
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class NewsItem(scrapy.Item):
name = scrapy.Field()
class StationDetailSpider(CrawlSpider):
name = 'test2'
start_urls = ["http://www.moneycontrol.com/india/stockpricequote/"]
rules = (
Rule(LinkExtractor(restrict_xpaths="//a[@class='bl_12']"), follow=False, callback='parse_news'),
Rule(LinkExtractor(allow=r"/diversified/.*$"), callback='parse_news')
)
def parse_news(self, response):
item = NewsItem()
NEWS1_SELECTOR = 'div#disp_nse_hist tr:nth-child(5) > td:nth-child(4)::text'
TIME1_SELECTOR = 'div#disp_nse_hist tr:nth-child(5) > td:nth-child(4)::text'
NAME_SELECTOR = 'div#disp_nse_hist tr:nth-child(5) > td:nth-child(4)::text'
print("------------------------------------starting extraction------------")
item['name']=response.css(NAME_SELECTOR).extract_first()
item['time1']=response.css(TIME1_SELECTOR).extract_first()
item['news1']=response.css(NEWS1_SELECTOR).extract()
return item
【问题讨论】:
-
你使用什么环境进行抓取?如果页面像你说的那样是动态的,你将需要像 phantomjs 这样的东西
-
我在 python3 (linux) 上使用 scrapy。
-
哦,原来是python的问题,跟javascript没关系,因为python没有javascript引擎
-
所以我无法使用 python 从 html 中提取 javscript 结果?
-
我非常怀疑
标签: javascript json xpath web-scraping scrapy