这是我使用任何类型的编码所做的第一件事,并且已经停留了一段时间。我可以运行此代码,并在Python Shell中查看结果,但是csv文件中没有数据。
import csv
import urllib .request
from bs4 import BeautifulSoup
filename = "dataoutput2.csv"
f = open(filename, "w")
f.write("")
soup = BeautifulSoup(urllib.request.urlopen("https://www.fangraphs.com/dailyprojections.aspx?pos=all&stats=pit&type=sabersim&team=0&lg=all&players=0").read(),'lxml')
tbody = soup('table',{"class":"rgMasterTable"})[0].find_all('tr')
for row in tbody:
cols = row.findChildren(recursive=False)
cols =[ele.text.strip() for ele in cols]
f.write
print(cols)
f.close()
这在csv文件中什么也没有给我,如果我尝试f.write(cols),我会得到
enter code here
第14行,在enter code here
f.write(cols)enter code here
TypeError:write()参数必须为str,而不是列表“名称”,“团队”,“游戏”,“ W”,“ IP”,“ TBF”,“ H”,“ 1B”,“ 2B”,“ 3B”,“ HR”, 'BB','SO','Yahoo','FanDuel','DraftKings'] ['Aaron Nola', 'Phillies','PHI @ SD','。48','6.50','26.73','5.43','3.43','。99', '.06','。96','2.31','7.72','24.77','40.23','22.41'] ['Walker Buehler”,“道奇队”,“ LAD @ ARI”,“。47”,“ 6.49”,“ 26.60”,“ 5.77”, '3.66','1.25','。23','。64','1.90','6.59','22.68','37.04', '20 .28'] ['Robbie Ray','Diamondbacks','LAD @ ARI','。27','5.78', '25 .35','4.74','2.82','。98','。13','。81','3.76','8.18','21.68', '37 .05','19 .94']
这是我打印到python shell的结果。我希望此行和列结束。我将不胜感激任何帮助。预先谢谢你。