【发布时间】:2019-11-04 01:04:28
【问题描述】:
一个正在进行的项目......由一个python新手!我从学校网站创建了 4 个“类 'bs4.element.ResultSet”,称为游戏(获胜)、平局、平局和自定义。我通过抓取所有学校分数并汇总来帮助联盟。我不知道如何将这 4 个 element.resultsets 组合在一起,以便我可以运行程序的其余部分。现在它只将“游戏(获胜)”保存到 excel 电子表格中。同样在下面的输出中还有大量空格 - 我怎样才能摆脱那些 \n\t ?非常感谢您的帮助。
from bs4 import BeautifulSoup as bs
import requests
import pandas as pd
import re
url = 'https://www.loomischaffee.org/athletics/teams/fall/soccer-boys/varsity'
page = requests.get(url)
soup = bs(page.content, 'html.parser')
week = soup.find(id='fsEl_5138')
games = week.find_all(class_ ='fsResultWin')
draws = week.find_all(class_ ='fsResultTie')
ties = week.find_all(class_ ='fsResultLoss')
custom = week.find_all(class_ ='fsResultCustom')
# now creating 6 lists of the data contained in the above.
date = [games.find(class_ = 'fsDate').get_text() for games in games]
time = [games.find(class_ = 'fsTime').get_text() for games in games]
opponent = [games.find(class_ = 'fsAthleticsOpponentName').get_text() for games in games]
home_away = [games.find(class_ = 'fsAthleticsAdvantage').get_text() for games in games]
location = [games.find(class_ = 'fsAthleticsLocations').get_text() for games in games]
result = [games.find(class_ = 'fsAthleticsResult').get_text() for games in games]
score = [games.find(class_ = 'fsAthleticsScore').get_text() for games in games]
# now I turn data into a table using pandas so I can manipulate
results = pd.DataFrame(
{'Date': date,
'Time': time,
'Opponent': opponent,
'Home/Away': home_away,
'Location' : location,
'Result': result,
'Score': score,
})
print(results)
results.to_excel('results.xls')
【问题讨论】:
-
使用正则表达式替换包含 \n\t 的字符串
标签: python-3.x pandas dataframe python-requests