Python 将刮取的表转换为数据帧时的非类型对象

Python 将刮取的表转换为数据帧时的非类型对象,python,beautifulsoup,Python,Beautifulsoup,我正在尝试取消显示在以下链接中的表格中的股票报价器列表: 我使用beautiful soup废弃了该表,但当我将其转换为Pandas数据帧时,我得到一个错误:TypeError:“NoneType”对象不可调用 我尝试了以下代码: url = 'http://www.advfn.com/nyse/newyorkstockexchange.asp?companies=A' res = requests.get(url) soup = BeautifulSoup(res.content,'lxml'

我正在尝试取消显示在以下链接中的表格中的股票报价器列表: 我使用beautiful soup废弃了该表,但当我将其转换为Pandas数据帧时,我得到一个错误:TypeError:“NoneType”对象不可调用

我尝试了以下代码:

url = 'http://www.advfn.com/nyse/newyorkstockexchange.asp?companies=A'
res = requests.get(url)
soup = BeautifulSoup(res.content,'lxml')
table = soup.find("table",{"class":"market tab1"})
df = pd.read_html(table)
但是它不起作用,怎么解决呢?为什么我会出错

完整错误日志:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs)
    796         try:
--> 797             tables = p.parse_tables()
    798         except Exception as caught:

~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in parse_tables(self)
    212     def parse_tables(self):
--> 213         tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
    214         return (self._build_table(table) for table in tables)

~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in _build_doc(self)
    618                 # try to parse the input in the simplest way
--> 619                 r = parse(self.io, parser=parser)
    620             try:

~/anaconda3/lib/python3.7/site-packages/lxml/html/__init__.py in parse(filename_or_url, parser, base_url, **kw)
    939         parser = html_parser
--> 940     return etree.parse(filename_or_url, parser, base_url=base_url, **kw)
    941 

src/lxml/etree.pyx in lxml.etree.parse()

src/lxml/parser.pxi in lxml.etree._parseDocument()

TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-23-c3e05c494f63> in <module>
      5 table = soup.find("table",{"class":"market tab1"})
      6 #print(table)
----> 7 df = pd.read_html(table)

~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in read_html(io, match, flavor, header, index_col, skiprows, attrs, parse_dates, tupleize_cols, thousands, encoding, decimal, converters, na_values, keep_default_na, displayed_only)
    985                   decimal=decimal, converters=converters, na_values=na_values,
    986                   keep_default_na=keep_default_na,
--> 987                   displayed_only=displayed_only)

~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs)
    799             # if `io` is an io-like object, check if it's seekable
    800             # and try to rewind it before trying the next parser
--> 801             if hasattr(io, 'seekable') and io.seekable():
    802                 io.seek(0)
    803             elif hasattr(io, 'seekable') and not io.seekable():

TypeError: 'NoneType' object is not callable
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
解析中的~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py(味道、io、匹配、属性、编码、仅显示、**kwargs)
796尝试:
-->797 tables=p.parse_tables()
798除捕获的异常外:
解析表中的~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py(self)
212 def parse_表(自):
-->213 tables=self.\u parse\u tables(self.\u build\u doc(),self.match,self.attrs)
214返回(表中的表的自构建表(表)
~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in\u build\u doc(self)
618#尝试以最简单的方式解析输入
-->619 r=parse(self.io,parser=parser)
620尝试:
解析中的~/anaconda3/lib/python3.7/site packages/lxml/html/\uuuuuu init\uuuuuuuuuuuuuuuu.py(文件名或url,解析器,基本url,**kw)
939解析器=html_解析器
-->940返回etree.parse(文件名\u或\u url,解析器,base\u url=base\u url,**kw)
941
lxml.etree.parse()中的src/lxml/etree.pyx
lxml.etree.\u parseDocument()中的src/lxml/parser.pxi
TypeError:“非类型”对象不可调用
在处理上述异常期间,发生了另一个异常:
TypeError回溯(最近一次调用上次)
在里面
5 table=soup.find(“table”,{“class”:“market tab1})
6#打印(表格)
---->7 df=pd.read\u html(表格)
读取html格式的~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py(io、匹配、味道、标题、索引列、skiprows、属性、解析日期、元组列、千、编码、十进制、转换器、na值、保留默认值、仅显示)
985十进制=十进制,转换器=转换器,na_值=na_值,
986保留默认值=保留默认值,
-->987仅显示=仅显示)
解析中的~/anaconda3/lib/python3.7/site-packages/pandas/io/html.py(味道、io、匹配、属性、编码、仅显示、**kwargs)
799#如果'io'是一个类似io的对象,请检查它是否可查找
800#并尝试在尝试下一个解析器之前倒带它
-->801如果hasattr(io,'seekable')和io.seekable():
802 io.seek(0)
803 elif hasattr(io,“可搜索”)而非io.seekable():
TypeError:“非类型”对象不可调用
表格编号:

<table cellpadding="0" cellspacing="1" class="market tab1" width="610">
<colgroup><col/><col/><col class="c"/></colgroup>
<tr><td class="tabh" colspan="3"><b>Companies listed on the NYSE</b></td></tr>
<tr><th>Equity</th><th>Symbol</th><th>Info</th></tr>
<tr class="ts0"><td align="left"><a href="http://ih.advfn.com/stock-market/NYSE/a-k-steel-AKS/stock-price">A K Steel</a></td><td><a href="http://ih.advfn.com/stock-market/NYSE/a-k-steel-AKS/stock-price">AKS</a></td><td><a href="http://ih.advfn.com/stock-market/NYSE/a-k-steel-AKS/chart"><img src="/s/stock-chart.gif"/></a><a href="http://ih.advfn.com/stock-market/NYSE/a-k-steel-AKS/news"><img src="/s/stock-news.gif"/></a><a href="http://ih.advfn.com/stock-market/NYSE/a-k-steel-AKS/financials"><img src="/s/fundamentals.gif"/></a><a href="http://ih.advfn.com/stock-market/NYSE/a-k-steel-AKS/trades"><img src="/s/stock-trades.gif"/></a></td></tr>

在纽约证券交易所上市的公司
EquitySymbolInfo
您正在传递一个
元素 进入pandas
阅读\u html
。您需要将其转换为
字符串

from bs4 import BeautifulSoup
import requests
import pandas as pd
url = 'http://www.advfn.com/nyse/newyorkstockexchange.asp?companies=A'
res = requests.get(url)
soup = BeautifulSoup(res.content,'lxml')
table = soup.find("table",{"class":"market tab1"})
df = pd.read_html(str(table))
print(df)
产出:

[                                    0       1     2
0        Companies listed on the NYSE     NaN   NaN
1                              Equity  Symbol  Info
2                           A K Steel     AKS   NaN
3                               A M R     AMR   NaN
4                      A M R Cp 7.875     AAR   NaN
5                               A V X     AVX   NaN
6                               A a R     AIR   NaN
7               A.h. Belo Corporation     AHC   NaN
8                         Aaron Rents   RNT.A   NaN
9                         Aaron Rents     RNT   NaN
10                        Aarons Cl A   AAN.A   NaN
11                        Aarons Inc.     AAN   NaN
12               Ab Svensk Cdss Arbmn     CBJ   NaN
13                   Ab Svensk Ekport     AXF   NaN
14               Ab Svensk Ekportkrdt     SQT   NaN
15               Ab Svensk Ekportkred     DVK   NaN
16               Ab Svensk Ekportkred     IWK   NaN
17               Ab Svensk Ekportkred     RCW   NaN
18               Ab Svensk Ekportkred     EOA   NaN
19                 Ab Svensk Msci Arn     MIS   NaN
20                  Ab Svensk Russell     REU   NaN
21                  Ab Svensk Sp Arns     SAD   NaN
22                  Ab Svensk Sp Arns     MHG   NaN
23                                Abb     ABB   NaN
24                        Abbott Labs     ABT   NaN
25                Abercrombie & Fitch     ANF   NaN
26                            Abitibi     ABY   NaN
27                                Abm     ABM   NaN
28                             Acadia     AKR   NaN
29                  Acc Bear Amex Egy     IMW   NaN
..                                ...     ...   ...
194                           Ashland     ASH   NaN
195                   Aspen Insurance     AHL   NaN
196  Assisted Living Concepts (nevada     ALC   NaN
197                Associated Estates     AEC   NaN
198                          Assurant     AIZ   NaN
199                  Assured Guaranty     AGO   NaN
200                           Astoria      AF   NaN
201                       Astrazeneca     AZN   NaN
202                 Atlanta Gas Light     ATG   NaN
203                    Atlas Pipeline     APL   NaN
204        Atlas Pipeline Holdings Lp     AHD   NaN
205                             Atmos     ATO   NaN
206                               Att       T   NaN
207                               Att     ATT   NaN
208                   Atwood Oceanics     ATW   NaN
209                      Au Optronics     AUO   NaN
210                           Autoliv     ALV   NaN
211                        Autonation      AN   NaN
212                          Autozone     AZO   NaN
213              Av Svensk Ekportkred     NEH   NaN
214                         Avalonbay     AVB   NaN
215              Aventine Renew Enrgy     AVR   NaN
216                    Avery Dennison     AVY   NaN
217                  Avis Budget Grp.     CAR   NaN
218                            Avista     AVA   NaN
219                             Avnet     AVT   NaN
220                     Avon Products     AVP   NaN
221                               Axa     AXA   NaN
222                              Axis     AXS   NaN
223                               Azz     AZZ   NaN

[224 rows x 3 columns]]

您得到错误是因为前面的一行代码返回了
None
。您可能应该验证您是否从
res
soup
table
中得到了预期的结果。我没有从res、soup和table中得到任何结果。我得到了res202,带有HTML的soup,在正确的表中…您让我们猜测错误发生在哪里。请发布完整准确的错误消息。这也有助于提供完整的错误回溯,以及
print(table)
I添加的完整日志中的一些数据。。