我想从 easydrop.ru 网站上删除一些数据。我没有太多的经验,所以,我遇到了一些问题。
第 1 步: 首先,我尝试使用 requests lib 中的 requests.get
加载并解析此页面,并使用 lxml 解析它。那是失败的,因为大部分内容都是 JS 生成的。所以我只有一小部分,主要是有关页面的信息。
第 2 步:我的第二次尝试是使用 PyQt 应用程序生成 JS:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
from lxml import html
# Take this class for granted.Just use result of rendering.
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
def getFinalHtml(url):
r = Render(url)
result = r.frame.toHtml()
final = html.fragment_fromstring(result, 'root')
return final
这样更好,但仍然不成功 - 页面现在很好,但是页面不包含任何数据(当前用户数、项目数等)
该网站使用Meteor框架,并且通过与他们通信而获得值(value),而无需重新加载页面。
第3步:目前我陷入了死胡同,无法想象如何获取页面内容以进行进一步解析:(
如何从此页面获取数据我只有两个想法:
1) 在Python中加载页面(步骤2的扩展)并使用lxml对它们进行解析(不幸的是,我不知道如何改进加载器以从 meteor 获取值)
2) 使用Python中的meteor函数从该站点(用户\项目)加载数据,无需html
我对第二种解决方案有一些想法 我正在尝试用 https://github.com/hharnisc/python-meteor 做一些事情库。但即使你尝试开始,我也陷入了死胡同:
我在这个网站上有一行 .js 脚本:
Meteor.startup(function (e) {
return function () {
return t(), Meteor.setInterval(t, 1e3), e.socket = io("https://ws.easydrop.ru", {transports: ["websocket"]})
}
所以,我尝试了一些变体
client = MeteorClient('http://ws.easydrop.ru:3000/websocket')
client = MeteorClient('http://ws.easydrop.ru:3000/')
client = MeteorClient('http://ws.easydrop.ru:443/')
client = MeteorClient('http://ws.easydrop.ru:443/websocket/')
但是我在 py 中有一个错误:
raise ValueError("Invalid scheme: %s" % scheme)
ValueError: Invalid scheme: https
我应该做什么来使用它?
最佳答案
Selenium 或 phantomjs 对此效果很好,这里有一个方便的指南:
http://toddhayton.com/2015/02/03/scraping-with-python-selenium-and-phantomjs/
关于javascript - 使用 Python 抓取 Meteor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098627/