首页 > 解决方案 > 尝试抓取表格时出错

问题描述

我正在尝试从网页获取一些数据,但我不断收到错误消息。尝试搜索谷歌,但我似乎无法找到一个好的解决方案。

url = "https://coinmarketcap.com/currencies/bitcoin/historical-data/"
content = requests.get(url).content
soup = BeautifulSoup(content,'html.parser')
table = soup.find('table', {'class': 'table'})
print(table)


data = [[td.text.strip() for td in tr.findChildren('td')]
        for tr in table.findChildren('tr')]

输出:

Traceback (most recent call last):
  File "C:/Users/Ejer/PycharmProjects/pythonProject/stock_analysis.py", line 13, in <module>
    for tr in table.findChildren('tr')]
AttributeError: 'NoneType' object has no attribute 'findChildren'

标签: pythonjsonbeautifulsoup

解决方案


你需要把表的类,而不仅仅是表,在你的情况下它有cmc-table

table = soup.find('table', {'class': 'cmc-table'}

试试这个它得到桌子


推荐阅读