我是python的新手。有谁知道{sum(int(int(td.text)for td在汤中。select('td:last-child')[1:])}}在此或[0]或[1中] [1:]的用途是什么]。我在下面的许多循环示例中看到了它。在我练习的过程中,我构建了此代码,无法将csv文件中的所有数据都删除。在此先感谢,一次抱歉两个问题。
import requests
from bs4 import BeautifulSoup
import csv
url= "https://iplt20.com/stats/2020/most-runs"
r= requests.get (url)
soup= BeautifulSoup (r.content, 'html5lib')
lst= []
table=soup.find ('div', attrs = {'class':'js-table'})
#for row in table.findAll ('div', attrs= {'class':'top-players__player-name'}):
# score = {}
# score['Player'] = row.a.text.strip()
# lst.append(score)
for row in table.findAll (class_='top-players__m top-players__padded '):
score = {}
score['Matches'] = int(row.td.text)
lst.append(score)
filename= 'iplStat.csv'
with open (filename, 'w', newline='') as f:
w= csv.DictWriter(f,['Player', 'Matches'])
w.writeheader()
for score in lst:
w.writerow(score)
print (lst)