gpt4 book ai didi

python - Beautifulsoup 结果到 Pandas 数据框

转载 作者:太空狗 更新时间:2023-10-30 00:59:23 25 4
gpt4 key购买 nike

下面的代码返回一个,结果如下

r = requests.get(url)
soup = bs4.BeautifulSoup(r.text, 'lxml')

mylist = soup.find(attrs={'class': 'table_grey_border'})
print(mylist)

结果 - 它延伸了 1700 行

<table cellpadding="0" cellspacing="2" class="table_grey_border" width="100%">
<tr valign="top">
<td class="verd_black12" width="18%"><b>STOCK CODE</b></td>
<td class="verd_black12" width="42%"><b>NAME OF LISTED SECURITIES</b></td>
<td class="verd_black12" width="19%"><b>BOARD LOT</b></td>
<td class="verd_black12" colspan="4" width="12%"><b>REMARK</b></td>
</tr>
<tr class="tr_normal">
<td class="verd_black12" width="18%">00001</td>
<td class="verd_black12" width="42%"><a href="../../../invest/company/profile_page_e.asp?WidCoID=00001&amp;WidCoAbbName=&amp;Month=&amp;langcode=e" target="_parent">CKH HOLDINGS</a></td>
<td class="verd_black12" width="19%">500</td>
<td align="center" class="verd_black12" width="3%">#</td>
<td align="center" class="verd_black12" width="3%">H</td>
<td align="center" class="verd_black12" width="3%">O</td>
<td align="center" class="verd_black12" width="3%">F</td>
</tr>
<tr class="tr_normal">
<td class="verd_black12" width="18%">00002</td>
<td class="verd_black12" width="42%"><a href="../../../invest/company/profile_page_e.asp?WidCoID=00002&amp;WidCoAbbName=&amp;Month=&amp;langcode=e" target="_parent">CLP HOLDINGS</a></td>
<td class="verd_black12" width="19%">500</td>
<td align="center" class="verd_black12" width="3%">#</td>
<td align="center" class="verd_black12" width="3%">H</td>
<td align="center" class="verd_black12" width="3%">O</td>
<td align="center" class="verd_black12" width="3%">F</td>
</tr>
...

我的问题是,如何将这些行中的每一行放入 Pandas Dataframe 中?我尝试了下面的代码,但返回错误

a = pandas.read_html(mylist)
print(a)

错误

TypeError: 'NoneType' object is not callable

最佳答案

Document :

pandas.read_html(url, attrs={'class': 'table_grey_border'})

关于python - Beautifulsoup 结果到 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42050796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com