f。只写出beautifulsoup的最后一行数据

时间:2018-08-10 23:30:21

标签: python beautifulsoup

我使用beautifulsoup4进行了抓取,当我将汤写入文件时,我只想编写我已抓取的内容。我遇到的问题是,仅抓取的最后一行数据正在写入。

  from bs4 import BeautifulSoup
  import requests

  source = requests.get('http://www.livescores.com/soccer/england/premier-league/').text

  soup = BeautifulSoup(source, 'lxml')

  tags = soup.findAll('div', class_='row-gray')

  for tag in tags:

    print(tag.getText())

 f = open("live.txt","w")
 f.write(tag.getText())

这就是我试图使文本文件看起来像

的内容
    FT   Manchester United   2 - 1   Leicester City
    11:30   Newcastle United   ? - ?   Tottenham Hotspur
    14:00   AFC Bournemouth   ? - ?   Cardiff City
    14:00   Fulham   ? - ?   Crystal Palace
    14:00   Huddersfield Town   ? - ?   Chelsea
    14:00   Watford   ? - ?   Brighton & Hove Albion
    16:30   Wolverhampton Wanderers   ? - ?   Everton
    12:30   Liverpool   ? - ?   West Ham United
    12:30   Southampton   ? - ?   Burnley
    15:00   Arsenal   ? - ?   Manchester City
   # Team Name P W D L F A GD Pts
     1  Manchester United 1 1 0 0 2 1 1 3
     2  AFC Bournemouth 0 0 0 0 0 0 0 0
     2  Arsenal 0 0 0 0 0 0 0 0
     2  Brighton & Hove Albion 0 0 0 0 0 0 0 0
     2  Burnley 0 0 0 0 0 0 0 0
     2  Cardiff City 0 0 0 0 0 0 0 0
     2  Chelsea 0 0 0 0 0 0 0 0
     2  Crystal Palace 0 0 0 0 0 0 0 0
     2  Everton 0 0 0 0 0 0 0 0
     2  Fulham 0 0 0 0 0 0 0 0
     2  Huddersfield Town 0 0 0 0 0 0 0 0
     2  Liverpool 0 0 0 0 0 0 0 0
     2  Manchester City 0 0 0 0 0 0 0 0
     2  Newcastle United 0 0 0 0 0 0 0 0
     2  Southampton 0 0 0 0 0 0 0 0
     2  Tottenham Hotspur 0 0 0 0 0 0 0 0
     2  Watford 0 0 0 0 0 0 0 0
     2  West Ham United 0 0 0 0 0 0 0 0
     2  Wolverhampton Wanderers 0 0 0 0 0 0 0 0
     20  Leicester City 1 0 0 1 1 2 -1 0

相反,我只是将其作为唯一的一行数据输入到我的输出文件中。

     20  Leicester City 1 0 0 1 1 2 -1 0

0 个答案:

没有答案