网站使用javascript

时间:2018-10-31 14:53:45

标签: python web-scraping

我要实现的目标

我正在尝试使用Beautiful-soup刮擦下面的网站,当我加载页面时,它没有给出显示各种报价的表格。在我以前的文章中,人们帮助我提供了实际提供主要网站的网站,但是我不确定他们是如何找到它的。一旦我提取了数据,其余的工作就可以完成。

网站

https://www.cmegroup.com/trading/energy/refined-products/methanol-t2-fob-rdam-icis.html

尝试过的东西。

我尝试使用Selenium驱动程序,但是遇到了不同的错误,这可能需要更多的时间并且不习惯使用Selenium。最终,我计划创建一个将信息下载到excel文件的exe。

3 个答案:

答案 0 :(得分:1)

网络上的许多页面都使用JS来更改页面。这些更改对Beautiful-soup不可见,因为它不执行JS。我可以想到两种选择:

  • 您可以使用Selenium之类的工具来实际运行带有JS的完整浏览器。
  • 您可以在Chrome或Firefox中打开网站,打开Web检查器,而不刷新页面。在“网络”选项卡中监视XHR请求,您可能会找到带您要查找的数据的请求。如果找到它,则可以直接加载该页面而不是主页。

答案 1 :(得分:0)

有什么方法可以运行实际上在页面上执行javascript的Python Web Client,然后可以抓取结果?

答案 2 :(得分:0)

如果您对硒不满意,请使用PyQt:

"""
Install PyQt on Ubuntu:
    sudo apt-get install python3-pyqt5
    sudo apt-get install python3-pyqt5.qtwebengine
or on other OS (64 bit versions of Python)
    pip3 install PyQt5
"""

from bs4 import BeautifulSoup
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView



class Render(QWebEngineView):
    def __init__(self, url):
        self.html = None
        self.app = QApplication(sys.argv)
        QWebEngineView.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.load(QUrl(url))
        self.app.exec_()

    def _loadFinished(self, result):
        self.page().toHtml(self.callable)

    def callable(self, data):
        self.html = data
        self.app.quit()


url = 'https://www.cmegroup.com/trading/energy/refined-products/methanol-t2-fob-rdam-icis.html'
html_source = Render(url).html
soup = BeautifulSoup(html_source, 'html.parser')
table = soup.find('table', {'id': 'quotesFuturesProductTable1'})
for tr in table.find_all('tr'):
    print(tr.get_text(" ", strip=True))

输出:

Month Charts Last Change Prior Settle Open High Low Volume Hi / Low Limit Updated
NOV 2018 Show Price Chart - - 357.00 - - - 0 No Limit / No Limit 18:01:39 CT 31 Oct 2018
DEC 2018 Show Price Chart - - 357.00 - - - 0 No Limit / No Limit 18:01:39 CT 31 Oct 2018
JAN 2019 Show Price Chart - - 345.00 - - - 0 No Limit / No Limit 18:01:39 CT 31 Oct 2018
FEB 2019 Show Price Chart - - 345.00 - - - 0 No Limit / No Limit 18:01:36 CT 31 Oct 2018
MAR 2019 Show Price Chart - - 342.00 - - - 0 No Limit / No Limit 18:02:29 CT 31 Oct 2018
APR 2019 Show Price Chart - - 339.00 - - - 0 No Limit / No Limit 18:01:47 CT 31 Oct 2018
MAY 2019 Show Price Chart - - 334.00 - - - 0 No Limit / No Limit 18:03:23 CT 31 Oct 2018
JUN 2019 Show Price Chart - - 334.00 - - - 0 No Limit / No Limit 18:01:53 CT 31 Oct 2018
JUL 2019 Show Price Chart - - 337.00 - - - 0 No Limit / No Limit 16:45:00 CT 31 Oct 2018
AUG 2019 Show Price Chart - - 337.00 - - - 0 No Limit / No Limit 16:45:00 CT 31 Oct 2018
SEP 2019 Show Price Chart - - 335.00 - - - 0 No Limit / No Limit 16:45:00 CT 31 Oct 2018
OCT 2019 Show Price Chart - - 335.00 - - - 0 No Limit / No Limit 16:45:00 CT 31 Oct 2018
NOV 2019 Show Price Chart - - 335.00 - - - 0 No Limit / No Limit 16:45:00 CT 31 Oct 2018
DEC 2019 Show Price Chart - - 335.00 - - - 0 No Limit / No Limit 16:45:00 CT 31 Oct 2018

一些警告也发送到标准错误。