我试图刮掉从以下链接下面的SVG的:刮SVG图表
https://finance.yahoo.com/quote/AAPL/analysts?p=AAPL
我想刮的部分如下:
我做不需要图表中的单词(只是图表本身)。但是,我从来没有刮过一张svg图像,我不确定是否有可能。我环顾四周,但找不到任何有用的python包来直接做到这一点。
我知道我可以使用硒使用Python的图像截图,然后使用PIL裁剪它并将其保存为svg,但我想知道是否有更直接的方式来抓住这些图表关闭页。任何有用的软件包或实现都将有所帮助。谢谢。
编辑:有一些下降,但票不知道为什么,这是我将如何实现它在我的方式..
import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
class Screenshot(QWebView):
def __init__(self):
self.app = QApplication(sys.argv)
QWebView.__init__(self)
self._loaded = False
self.loadFinished.connect(self._loadFinished)
def capture(self, url, output_file):
self.load(QUrl(url))
self.wait_load()
# set to webpage size
frame = self.page().mainFrame()
self.page().setViewportSize(frame.contentsSize())
# render image
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
print 'saving', output_file
image.save(output_file)
def wait_load(self, delay=0):
# process app events until page loaded
while not self._loaded:
self.app.processEvents()
time.sleep(delay)
self._loaded = False
def _loadFinished(self, result):
self._loaded = True
s = Screenshot()
s.capture('https://finance.yahoo.com/quote/AAPL/analysts?p=AAPL', 'yhf.png')
然后,我会使用PIL作物功能,所拍摄的图像出来的图表。
所以你只需要得到该SVG文件并将其转换为PNG? –
你的问题是那些SVG正在由Javascript生成。任何解决方案不仅涉及加载HTML,还涉及在从DOM提取内联SVG之前执行页面上的所有JS。一个简单的HTML刮板不会满足您的需求。 –