python - 尝试将抓取的数据添加到列表时仅返回一个值,Python
问题描述
我正在尝试从网站上抓取数据,我的代码的第一部分似乎有效。但是,当我尝试将该数据添加到列表中时,它仅从抓取的数据中返回一个值。我想也许它被识别为字符串,尝试了分隔符,但没有用。请帮忙!
我使用了 Goodgle Colab
提前致谢!
import requests
from bs4 import BeautifulSoup
url = "https://athletics.baruch.cuny.edu/sports/mens-swimming-and-diving/roster"
page = requests.get(url)
page.content ##content of classes, for the problem we need "sidearm-roster-player-height" class
soup = BeautifulSoup(page.content, "html.parser")
height_swimmers = soup.findAll('span', class_ = 'sidearm-roster-player-height')
print(height_swimmers)
for text in height_swimmers:
height = text.get_text()
print(height)
height_list = []
height_list.append(height)
print(height_list)
解决方案
在 for 循环之后,您只需添加height
to的最后一个值height_list
。
为了在列表中打印所有抓取的结果,请使用
height_list = []
for text in height_swimmers:
height = text.get_text()
print(height)
height_list.append(height)
代替
for text in height_swimmers:
height = text.get_text()
print(height)
height_list = []
height_list.append(height)
推荐阅读
- scala - 具有基础类型的类型参数 - Scala
- python - 在数据集 PYTHON 上使用正则表达式分隔
- vba - 最后将 Excel 工作表复制到另一个工作簿 (VBA)
- javascript - Discord.js SQLITE_ERROR:表分数没有名为点的列
- tensorflow - Compute Capability 3.0 卡可以运行 Tensorflow 1.8 tensorflow-gpu 运行时吗?
- regex - 使用正则表达式从字符串中提取文件名
- django - 如何在 Django 中将 URL 限制为管理员用户
- javascript - 边距:0 自动;用搜索隐藏另一个 li 元素后
- python - 日历项目没有正确缩进和间距 Python 3
- highcharts - 如何从 Highcharts 中删除注释?