这个问题已经在这里有一个答案:
我试图从一个网站抽取数据。我想提出一个循环来提取数据,并在一个变量存储,但不能将其保存在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)
任何帮助将不胜感激!
我发现做你的拆解和利用大熊猫将其保存在一个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")
这应该正常工作