【发布时间】:2019-03-15 05:05:51
【问题描述】:
我正在尝试将抓取的数据转换为 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
谢谢!
【问题讨论】:
-
请分享正在解析它的网页链接以查看标签布局,否则将很难提供帮助。
标签: python pandas beautifulsoup