Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用美丽的汤和熊猫从网页获取表格_Python_Beautifulsoup - Fatal编程技术网

Python 使用美丽的汤和熊猫从网页获取表格

Python 使用美丽的汤和熊猫从网页获取表格,python,beautifulsoup,Python,Beautifulsoup,使用Python 3.6.1运行代码 import requests import pandas as pd from bs4 import BeautifulSoup # url_addr = "https://www.uspto.gov/web/offices/ac/ido/oeip/taf/mclsstc/mcls1.htm" url_addr = "https://www.cefconnect.com/closed-end-funds-daily-pricing" html_text =

使用Python 3.6.1运行代码

import requests
import pandas as pd
from bs4 import BeautifulSoup
# url_addr = "https://www.uspto.gov/web/offices/ac/ido/oeip/taf/mclsstc/mcls1.htm"
url_addr = "https://www.cefconnect.com/closed-end-funds-daily-pricing"
html_text = requests.get(url_addr).content
bs_obj = BeautifulSoup(html_text)
tables = bs_obj.findAll('table')
dfs = list()
for table in tables:
    df = pd.read_html(str(table))[0]
    dfs.append(df)
    print(df)
仅获取列标题,但不获取实际数据,并提供输出

Empty DataFrame
Columns: [Ticker, Fund Name, Strategy, ClosingPrice, PriceChange, NAV, Premium/Discount, DistributionRate, DistributionRate on NAV, 1 Yr Rtnon NAV]
Index: []

它适用于注释掉的url地址。

第二个url用Javascript填充表格。如果您使用
wget
或查看Google Chrome中的“网络”选项卡,您将看到这是最初发送的表格(即,这是Beautive soup看到的):


股票行情
基金名称
策略
收盘价
价格变动
导航
溢价/
折扣 分布
速率和匕首; 分配
资产净值比率 资产净值上的1年Rtn
然后一些Javascript填充该表。这里有两个选项,要么使用(像PhantomJS、Selenium,有很多选项相对容易使用)并在解析之前运行Javascript,要么尝试找出如何访问页面用于添加数据的API


我经常提到的另一个选择是联系网站所有者,制定一个安排,以更直接的方式获取数据。

在阅读html之前,您是否从表格中获得任何输出?他们的一个输出是什么样子的?如果那里有输出,我猜它在读取html时失败了。我检查了网站,看起来您正在尝试访问动态生成的表。因此,
bs_obj=BeautifulSoup(html_text)
没有完全呈现,并且表实际上是空的。您应该使用能够完全呈现网页并将其传递到BeatifulSoup()的api。查看ScrapRapi或proxycrawl。
        <div id="data-container" class="row-fluid">
            <div class="span12">                    
                <table class="cefconnect-table-1 daily-pricing table table-striped table-condensed" id="daily-pricing" width="100%" cellpadding="5" cellspacing="0" border="0" summary="">
                    <thead>
                        <tr>
                            <th class="ticker">Ticker</th>
                            <th class="fund-name">Fund Name</th>
                            <th class="strategy">Strategy</th>
                            <th class="closing-price">Closing<br />Price</th>
                            <th class="price-change">Price<br />Change</th>
                            <th class="nav">NAV</th>
                            <th class="premium-discount">Premium/<br />Discount</th>
                            <th class="distribution-rate">Distribution<br />Rate<sup>&dagger;</sup></th>
                            <th class="distribution-rate-on-nav">Distribution<br />Rate on NAV</th>
                            <th class="return-on-nav">1 Yr Rtn<br />on NAV</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>