我正在尝试将抓取的数据转换为 pd 数据框(表)。 该信息是通过 beautifulsoup 从不同标签(a、span、div)检索的。 for ul in soup_level1.find('ul', {'class':"fix3"}):
divjt=ul.find('div',{'class':"topb"})
a=divjt.find('a')
trajectory=a.text.strip()
divloc=ul.find('div',{'class':"under"})
d=divloc.find('div')
sp=ul.find('span',{'class':"blk"})
object=sp.text.strip()
try:
sas=ul.find_all('span',{'class':"f1"})
timex=sas[0].text
except IndexError:
timex=''
datalist.append[jobtitle,city,timex]
headers=['Traj', 'Object', 'Time']
A=[trajectory]
B=[object]
C=[timex]
datac=A+B+C
df = pd.DataFrame(datac)
print(df)
我现在得到的结果是
0
0 BRD - TWD
1 MER
2 11/10/2018
0
0 SFX - NYT
1 MER
2 10/05/2016
0
0 GER - BEN
1 MER
2 05/06/2016
我基本上想将这些结果“转储”到正确的数据帧表中 每行都会相应地打印到 Excel。
0 BRD - TWD MER 11/10/2018
1 SFX - NYT MER 10/05/2016
2 GER - BEN MER 05/06/2016
谢谢!
最佳答案
如果您希望Excel中的数据改用csv格式,可以在excel/Libre Office中打开csv文件以获得所需的结果
var row = value1 + ":" + value2 + ":" + value3 ;
await fs.appendFile('file_name.csv', row + os.EOL, function (err) {
if (err) throw err;
});
这就是我在 Javascript 中的做法。
关于python - BeautifulSoup Python 到 Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737636/