首页 > 解决方案 > BeautifulSoup 不显示内容

问题描述

我想从 MCX India 网站上抓取现货价格数据。检查元素时可见的 HTML 脚本如下:

<div class="contents spotmarketprice">
            <div id="cont-1" style="display: block;">
                <table class="mcx-table mrB20" width="100%" cellspacing="8" id="tblSMP">
                    <thead>
                        <tr>
                            <th class="symbol-head">
                                Commodity
                            </th>
                            <th>
                                Unit
                            </th>
                            <th class="left1">
                                Location
                            </th>
                            <th class="right1">
                                Spot Price (Rs.)
                            </th>
                            <th>
                                Up/Down
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                      <tr>
                          <td class="symbol" style="width:30%;">ALMOND</td>
                          <td style="width:17%;">1 KGS</td>
                          <td align="left" style="width:17%;">DELHI</td>
                          <td align="right" style="width:17%;">558.00</td>  

                          <td align="right" class="padR20" style="width:19%;">=</td>                                         
                      </tr>

我写的代码是:

#import the required libraries    
from bs4 import BeautifulSoup
import requests

#Getting data from website
source= requests.get('http://www.mcxindia.com/market-data/spot-market-price').text

#Getting the html code of the website
soup = BeautifulSoup(source, 'lxml')

#Navigating to the blocks where required content is present
division_1= soup.find('div', class_="contents spotmarketprice").div.table

#Displaying the results
print(division_1.tbody)

输出:

<tbody>
   </tbody>

在网站上,我想要获取的内容可以在......但是,这里没有显示任何内容。请提出解决方案。

标签: pythonhtmlweb-scrapingbeautifulsouplxml

解决方案


表格中的数据似乎是通过 JavaScript 上传的。

这就是为什么,如果您尝试使用requests库获取此信息,则返回时不会收到表的数据。requests根本不支持JS。因此,这里的问题不在BeautifulSoup.

要抓取 JS 驱动的数据,请考虑使用seleniumchromedriver 。这种情况下的解决方案将如下所示:

# import libraries
from bs4 import BeautifulSoup
from selenium import webdriver

# create a webdriver
chromedriver_path = 'C:\\path\\to\\chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path)

# go to the page and get its source
driver.get('http://www.mcxindia.com/market-data/spot-market-price')
soup = BeautifulSoup(driver.page_source, 'html.parser')

# fetch mentioned data
table = soup.find('table', {'id': 'tblSMP'})
for tr in table.tbody.find_all('tr'):
    row = [td.text for td in tr.find_all('td')]
    print(row)

# close the webdriver
driver.quit()

上述脚本的输出是:

['ALMOND', '1 KGS', 'DELHI', '558.00', '=']
['ALUMINIUM', '1 KGS', 'THANE', '137.60', '=']
['CARDAMOM', '1 KGS', 'VANDANMEDU', '2,525.00', '=']
['CASTORSEED', '100 KGS', 'DEESA', '3,626.00', '▼']
['CHANA', '100 KGS', 'DELHI', '4,163.00', '▲']
['COPPER', '1 KGS', 'THANE', '388.30', '=']
['COTTON', '1 BALES', 'RAJKOT', '15,790.00', '▲']
['CPO', '10 KGS', 'KANDLA', '630.10', '▼']
['CRUDEOIL', '1 BBL', 'MUMBAI', '2,418.00', '▲']
['GOLD', '10 GRMS', 'AHMEDABAD', '40,989.00', '=']
['GOLDGUINEA', '8 GRMS', 'AHMEDABAD', '32,923.00', '=']
['GOLDM', '10 GRMS', 'AHMEDABAD', '40,989.00', '=']
['GOLDPETAL', '1 GRMS', 'MUMBAI', '4,129.00', '=']
['GUARGUM', '100 KGS', 'JODHPUR', '5,880.00', '=']
['GUARSEED', '100 KGS', 'JODHPUR', '3,660.00', '=']

UPD:我必须指定上面的代码回答了查看此特定表的问题。但是,有时网站将数据存储在“应用程序/json”或可以通过“请求”库访问的类似标签中(因为它们不需要 JS)。

αԋɱҽԃ αмєяιcαη发现,当前网站包含此类标签。请检查他的答案。确实requestsselenium在这种情况下使用 , 更好。


推荐阅读