如何在不删除旧数据的情况下追加新数据?(Python,Pandas;BeautifulSoup)



我要做的是,每次运行此文件时,它都会在不删除旧数据的情况下追加新数据。

当我以这种方式运行它时,它会完全删除旧文件并重写它

此外,如果您有任何其他可以改进代码的想法,如果您能分享,我将不胜感激。

提前感谢您的帮助。

from bs4 import BeautifulSoup
import requests
import pandas as pd
from datetime import date
import time
url = 'https://www.teknosa.com/laptop-notebook-c-116004?s=%3Arelevance%3Aseller%3Ateknosa&page={page}'
headers= {'User-Agent': 'Mozilla/5.0'}
data=[]
for page in range(1,6):
req=requests.get(url.format(page=page),headers=headers)
soup = BeautifulSoup(req.text, 'lxml')
jobs = soup.find_all('div', class_='prd')
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
for job in jobs:
data.append({
'Tarih': date.today(),
'Saat': current_time,
'Ürün Açıklaması' : job.find('a', class_='prd-link')['title'],
'Account Kod' : job.find('button', class_='prd-favorite btn-add-favorites')['data-product-id'],
'Fiyat' : job.find('span', class_='prc prc-last').text.strip(),
'URL': "https://www.teknosa.com" + job.find('a', class_='prd-link')['href'],
})
def append_df_to_excel(df, excel_path):
df_excel = pd.read_excel(excel_path)
result = pd.concat([df_excel, df], ignore_index=True)
result.to_excel(excel_path, index=False)
df = pd.DataFrame(data)
append_df_to_excel(df, r"test.xlsx")

编辑:大家好,

我发现了一个代码,它将部分解决我的问题,我想与您分享它,但现在我面临另一个问题。

每次运行该文件时,它都会破坏以前的时间格式,如示例所示。

错误示例

我不明白这个问题是与代码有关还是与excel错误有关,我感谢您的提前帮助。

编辑2:我为自己找到了解决方案,以防你需要我把它放在这里。

for pageteknosa in range(1,6):
reqteknosa=requests.get(urlteknosa.format(page=pageteknosa),headers=headers)
soupteknosa = BeautifulSoup(reqteknosa.text, 'lxml')
jobsteknosa = soupteknosa.find_all('div', class_='prd')
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
now = datetime.datetime.now()
for jobteknosa in jobsteknosa:
datateknosa.append({
'LFR' : "Teknosa",
'Kategori': "Notebook",
'Tarih': now.strftime("%Y-%m-%d"),
'Saat': current_time,
'Ürün Açıklaması' : jobteknosa.find('a', class_='prd-link')['title'],
'Account Kod' : int(jobteknosa.find('button', class_='prd-favorite btn-add-favorites')['data-product-id']),
'Fiyat': float(jobteknosa.find('span', class_='prc prc-last').text.replace(" TL",""))*1000,
'URL': "https://www.teknosa.com" + jobteknosa.find('a', class_='prd-link')['href']

}),

下面的代码将显示实际和正确的抓取方式。它将使您的代码和时间减少5倍。

from bs4 import BeautifulSoup
import requests
import pandas as pd
from datetime import date
import time
url = 'https://www.teknosa.com/laptop-notebook-c-116004?s=%3Arelevance%3Aseller%3Ateknosa&page={page}'
headers= {'User-Agent': 'Mozilla/5.0'}
data=[]
for page in range(1,6):
req=requests.get(url.format(page=page),headers=headers)
soup = BeautifulSoup(req.text, 'lxml')
jobs = soup.find_all('div', class_='prd')
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
for job in jobs:
data.append({
'Tarih': date.today(),
'Saat': current_time,
'xy' : job.find('a', class_='prd-link')['title'],
'Account Kod' : job.find('button', class_='prd-favorite btn-add-favorites')['data-product-id'],
'Fiyat' : job.find('span', class_='prc prc-last').text.strip(),
'URL': "https://www.teknosa.com" + job.find('a', class_='prd-link')['href'],
})
df=pd.DataFrame(data)#.to_excel('test.xlsx')
print(df)

最新更新