我用python编写了一个脚本,以从javascript呈现的网页中获得上一次交易的价格。如果我选择使用selenium,我可以获得内容。我在这里的目标是不使用像selenium之类的浏览器模拟器,因为请求-HTML的最新版本应该具有解析javascript加密内容的能力。然而,我无法成功地进行一次。当我运行这个脚本时,我会得到以下错误。在这方面的任何帮助都将受到高度赞赏。
网址:链接
我试过的剧本:
import requests_html
with requests_html.HTMLSession() as session:
r = session.get('https://www.gdax.com/trade/LTC-EUR')
js = r.html.render()
item = js.find('.MarketInfo_market-num_1lAXs',first=True).text
print(item)这是完整的回溯:
Exception in callback NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49
handle: <Handle NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49>
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 145, in _run
self._callback(*self._args)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 52, in watchdog_cb
self._timeout)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 40, in _raise_error
raise error
concurrent.futures._base.TimeoutError: Navigation Timeout Exceeded: 3000 ms exceeded
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\experiment.py", line 6, in <module>
item = js.find('.MarketInfo_market-num_1lAXs',first=True).text
AttributeError: 'NoneType' object has no attribute 'find'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 387, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\ar\\.pyppeteer\\.dev_profile\\tmp1gng46sw\\CrashpadMetrics-active.pma'我想要的价格可以在页面的顶部找到,它可以像这个177.59 EUR Last trade price一样可见。我希望得到177.59或任何目前的价格。
发布于 2018-03-03 22:18:33
你有几个错误。第一个是“导航”超时,显示页面没有完成呈现:
Exception in callback NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49
handle: <Handle NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49>
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 145, in _run
self._callback(*self._args)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 52, in watchdog_cb
self._timeout)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 40, in _raise_error
raise error
concurrent.futures._base.TimeoutError: Navigation Timeout Exceeded: 3000 ms exceeded此跟踪不会在主线程中引发,因此您的代码没有被中止。您的页面可能是完整的,也可能不是完整的;您可能希望设置一个更长的超时,或者为浏览器引入一个睡眠周期,以便有时间处理AJAX响应。
接下来,response.html.render()元素返回它将HTML加载到无头浏览器中,将JavaScript呈现留给该浏览器,然后将页面HTML复制回response.html数据结构中,不需要返回任何内容。因此,js设置为None,而不是一个新的HTML实例,从而导致您的下一个跟踪。
在呈现之后,使用现有的response.html对象进行搜索:
r.html.render()
item = r.html.find('.MarketInfo_market-num_1lAXs', first=True)很可能没有这样的CSS类,因为在通过AJAX加载JSON数据之后,每个页面呈现都会生成最后5个字符。这使得很难使用CSS来查找有问题的元素。
此外,我发现如果没有睡眠周期,浏览器就没有时间获取AJAX资源并呈现要加载的信息。比方说,在复制返回sleep之前,给它10秒的时间来做一些工作。如果看到网络超时,则设置更长的超时时间(默认为8秒):
r.html.render(timeout=10, sleep=10)您也可以将timeout设置为0,以删除超时并无限期地等待页面加载。
您可以使用图书馆找到匹配的CSS类:
# search for CSS suffixes
suffixes = [r[0] for r in r.html.search_all('MarketInfo_market-num_{:w}')]
for suffix in suffixes:
# for each suffix, find all matching elements with that class
items = r.html.find('.MarketInfo_market-num_{}'.format(suffix))
for item in items:
print(item.text)现在我们得到了产出:
169.81 EUR
+
1.01 %
18,420 LTC
169.81 EUR
+
1.01 %
18,420 LTC
169.81 EUR
+
1.01 %
18,420 LTC
169.81 EUR
+
1.01 %
18,420 LTC您的上一次跟踪显示,无法清除铬用户数据路径。底层Pyppeteer库使用临时用户数据路径配置无头铬浏览器,在您的情况下,该目录包含一些尚未锁定的资源。您可以忽略此错误,不过您可能希望稍后尝试删除.pyppeteer文件夹中的任何剩余文件。
发布于 2018-03-03 12:03:58
你需要它来通过请求-HTML吗?在你发布的那一天,回购已经有4天了,在过去的3天里,已经提交了50次。在一段时间内不会完全稳定。
见此处:https://github.com/kennethreitz/requests-html/graphs/commit-activity
OTOH,有一个用于gdax的API。
https://docs.gdax.com/#market-data
现在,如果您完全开始使用Py3,那么GDAX网站上列出了一个python客户端。首先,我将提到它是非官方客户端;但是,如果您使用它,您将能够快速、轻松地从官方的GDAX获得响应。
发布于 2018-02-28 07:42:40
如果您想通过运行Selenium抓取来使用另一种方式
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
chrome_path = r"C:\Users\Mike\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.gdax.com/trade/LTC-EUR")
item = driver.find_element_by_xpath('''//span[@class='MarketInfo_market-num_1lAXs']''')
item = item.text
print item
driver.close()结果:177.60欧元
https://stackoverflow.com/questions/49023861
复制相似问题