将刮取的表转换为DataFram时的非类型对象

2024-09-24 04:22:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试取消显示在以下链接中的表中的股票代码列表:http://www.advfn.com/nyse/newyorkstockexchange.asp?companies=A 我用BeautifulSoup废弃了这个表,但是当我把它转换成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)

但它不起作用-你知道怎么解决它吗?为什么我会出错?在

完整错误日志:

^{pr2}$

餐桌开始:

<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>

Tags: comhttpstocktableaksmarkettrclass
1条回答
网友
1楼 · 发布于 2024-09-24 04:22:41

您正在传递一个<class 'bs4.element.Tag'>元素 变成熊猫read_html。您需要将其转换为string。在

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)

输出:

^{pr2}$

相关问题 更多 >