【发布时间】:2018-01-13 06:14:23
【问题描述】:
所以我对 python 非常陌生,并且仍在努力了解一切是如何工作的,现在我正在使用漂亮的汤来抓取表格以获取数据。我可以使用漂亮的汤导航到我想要的特定表格,但是提取实际数据让我感到难过,我尝试的一切都失败了。
这是我当前的代码:
sauce = requests.get('https://www.investsmart.com.au/managed-funds/fund/cromwell-phoenix-opportunities-fund/40665')
soup = BeautifulSoup(sauce.text, 'html.parser')
tables = soup.findChildren('table')
my_table = tables[1]
rows = my_table.findChildren(['tr'])
for tds in rows[1]:
print(tds)
这留给我输出
<td class="text-left">Total return</td>
<td>-2.79</td>
<td>-2.61</td>
<td>11.22</td>
<td>24.6</td>
<td>19.18</td>
<td>18.65</td>
<td>21.44</td>
<td>-</td>
我想要的只是 td 标记内的实际数字,最终我想将其分类为各自的月份并将其输出到 excel 文件中。
但是当我尝试时,我不确定如何只获取没有标签的退货:
for tds in rows[1]:
print(tds.text)
我得到这个错误:AttributeError: 'NavigableString' object has no attribute 'text'
那么我该如何获取这些数据,以便我可以对其来源的月份进行分类并输出到 Excel,因为我不确定下一步该做什么。
【问题讨论】:
标签: python python-3.x beautifulsoup