使用 python 将数据写入 excel 或 csv 文件

Write data to excel or csv file using python

首先,我是 Python 的新手。话虽如此,我正在尝试抓取一个网页以查看 Shopkins Live 表演的时间和地点,以便我可以对其进行映射。现在,在你用 Shopkins 评判我之前——只要知道这是我和我最大的孩子一起做的一个项目,他是 Shopkins 的超级粉丝。

我最初尝试将数据放入 csv 文件,但当我这样做时,它会将单个字符打印到一列中 - 例如,它会说 - d e 跨四列而不是一列。我试着把它放在括号 writerows([data]) 中,但这没有用。

我现在已经尝试着写 excel,但正在为如何去做而苦恼。任何帮助将不胜感激。当我 运行 python 代码时,它在 Shell 中输出我需要的内容,但不确定如何转换它以在 Excel 中正确写入。以下是我的代码 - 任何帮助表示赞赏:

from bs4 import BeautifulSoup
import requests
import os, csv, sys, openpyxl
import openpyxl 
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
import contextlib
import re




url = 'http://www.shopkinslive.com/tour'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
wb = load_workbook('Shopkins.xlsx')
ws = wb.get_sheet_by_name('Sheet1')

for div in soup.find_all('div', class_='row'):
    divtext = (div.text)
    ws['A1'].value = divtext  #I know this is what I need to fix but not sure how to do it


    print(divtext)   #This part works correctly

你的孩子真的很幸运,我打赌他/她知道这一点!

对于代码:

from bs4 import BeautifulSoup
import requests
import pandas as pd


url = 'http://www.shopkinslive.com/tour'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")

tour_date = soup.find_all("div", {"class": "col date"})
tour_city = soup.find_all("div", {"class": "col city"})
tour_venue = soup.find_all("div", {"class": "col venue"})
tour_time = soup.find_all("div", {"class": "col time"})


t_date = [' '.join(td.text.split()[1:]) for td in tour_date]
t_city = [' '.join(td.text.split()[1:]) for td in tour_city]
t_venue = [' '.join(td.text.split()[1:]) for td in tour_venue]
t_time = [' '.join(td.text.split()[1:]) for td in tour_time]


df = pd.DataFrame({'Date' : t_date, 'City': t_city, 'Venue': t_venue, 'Time': t_time} )

df.to_excel('Shopkins.xlsx')

我试图解决这个问题,因为我没有设法逐行附加数据框。但我的想法是:

我将使用 BS 获取所有必要的数据,将它们放入单独的列表中,然后在导出到 excel 文件之前将它们合并到一个数据框中。

请查看是否满足您的需求。

P.s:[1:]部分应该去掉网站提供的<strong></strong>标签中的文字。所以是的,这就是模式。