首页 > 解决方案 > 如何在python中一一划分值?

问题描述

import requests
import json
from bs4 import BeautifulSoup

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'}
url = 'https://olympics.com/tokyo-2020/olympic-games/ko/results/all-sports/medal-standings.htm'
res = requests.get(url, headers = headers)
soup = BeautifulSoup(res.content, 'html.parser')
data = soup.select('td.text-center')

for item in data:
    text = item.get_text().replace(' ', '')
    print(text)

结果图片

我正在爬东京奥运会奖牌榜,我怎样才能改变这个像数组(或列表)?

标签: python

解决方案


添加text到 alist并打印。

在这里,我添加textres列表并打印它。

res = []
for item in data:
    text = item.get_text().strip().replace(' ', '')
    res.append(text)

# printing the res list
print(res)

推荐阅读