如何在不删除旧数据的情况下追加新数据? (Python,Pandas;BeautifulSoup)
How to append new data without deleting old ones? (Python,Pandas;BeautifulSoup)
我想做的是,每次这个文件 运行s,它都会在不删除旧数据的情况下追加新数据。
当我这样 运行 时,它会完全删除旧文件并重写它。
另外,如果您有任何其他我可以改进代码的想法,如果您能分享它们,我将不胜感激。
预先感谢您的帮助。
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")
编辑:大家好,
我找到了可以部分解决我的问题的代码,我想与您分享,但现在我面临另一个问题。
每次我 运行 文件时,它都会继续破坏以前的时间格式,如示例所示。
Error Example
我无法理解问题是与代码有关还是 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)
我想做的是,每次这个文件 运行s,它都会在不删除旧数据的情况下追加新数据。
当我这样 运行 时,它会完全删除旧文件并重写它。
另外,如果您有任何其他我可以改进代码的想法,如果您能分享它们,我将不胜感激。
预先感谢您的帮助。
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")
编辑:大家好,
我找到了可以部分解决我的问题的代码,我想与您分享,但现在我面临另一个问题。
每次我 运行 文件时,它都会继续破坏以前的时间格式,如示例所示。
Error Example
我无法理解问题是与代码有关还是 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)