在Python中将数组输出保存到csv

时间:2019-02-07 20:11:49

标签: python arrays file csv save

我正在尝试从网站上抓取数据。我正在制作一个循环以提取数据并将其存储在变量中,但是无法将其保存在csv文件中。刚接触Python和BeautifulSoup时,我的步伐还不算太远。这是代码:

import requests
from bs4 import BeautifulSoup
import csv

r = "https://sofia.businessrun.bg/en/results-2018/"
content = requests.get(r)

soup = BeautifulSoup(content.text, 'html.parser')


for i in range (1,5):
    team_name= soup.find_all(class_="column-3")
    team_time= soup.find_all(class_="column-5")


for i in range (1,5):
  print (team_name[i].text)
  print (team_time[i].text)

with open("new_file.csv","w+") as my_csv:
    csvWriter = csv.writer(my_csv,delimiter=',')
    csvWriter.writerows(team_name)

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我发现了另一种使用熊猫进行剪贴并将其保存在csv中的方法。代码如下:

import requests

# I changed this
import pandas as pd

from bs4 import BeautifulSoup
import csv

r = "https://sofia.businessrun.bg/en/results-2018/"
content = requests.get(r)

soup = BeautifulSoup(content.text, 'html.parser')


for i in range (1,5):
    team_name= soup.find_all(class_="column-3")
    team_time= soup.find_all(class_="column-5")

tn_list = []
tt_list = []

# I changed this to have string in place of tags 
tn_list = [str(x) for x in team_name]
tt_list = [str(x) for x in team_time]

for i in range (1,5):
    print(team_name[i].text)
    print(team_time[i].text)

# I put the result in a dataframe
df = pd.DataFrame({"teamname" : tn_list, "teamtime" : tt_list})

# I use regex to clean your data (get rid of the html tags)
df.teamname = df.teamname.str.replace("<[^>]*>", "")
df.teamtime = df.teamtime.str.replace("<[^>]*>", "")

# The first row is actually the column name
df.columns = df.iloc[0]
df = df.iloc[1:]

# I send it to a csv
df.to_csv(r"path\to\new_file.csv")

这应该正常工作