数据不写入excel表,只写入最后一行。尝试了不同的位置,结果相同。如果有人知道,请告诉我错误在哪里?
import pandas as pd
import requests
from bs4 import BeautifulSoup
while True:
print('Выберите категорию товара: легковые шины, грузовые шины, внедорожные шины')
name = input()
if name == 'легковые шины':
get_name = 'https://samara.express-shina.ru/search/legkovyie-shinyi'
elif name == 'грузовые шины':
get_name = 'https://samara.express-shina.ru/search/gruzovyie-shinyi'
elif name == 'внедорожные шины':
get_name = 'https://samara.express-shina.ru/search/vnedorozhnyie-shinyi'
else:
print('Такой категории нет')
print('Введите название файла латинскими буквами')
file_name = str(input())
count = 1
while count <= 3:
url = f'{get_name}?num={count}'
data = requests.get(url).text
block = BeautifulSoup(data, 'lxml')
heads = block.find_all('div', class_='b-offer__boxes')
for i in heads:
get_url = i.find_next('a').get('href')
# print('https://samara.express-shina.ru'+get_url)
w = ('https://samara.express-shina.ru' + get_url)
seac = requests.get(w).text
look = BeautifulSoup(seac, 'lxml')
leen = look.find('div', class_='header_product_page').find('h1')
print(leen.text.strip())
nazvan = (leen.text.strip())
price = look.find('span', class_='price_new')
print(price.text.strip())
cena = (price.text.strip())
articul = look.find('span', class_='articul')
print(articul.text.strip())
codde = (articul.text.strip())
img = look.find('div', class_='inner_images').find('img').get('src')
print('https://samara.express-shina.ru' + img)
pixx = ('https://samara.express-shina.ru' + img)
print('\n')
storage = {'zagol': nazvan,
'cena': cena,
'articul': codde,
'img': pixx}
df = pd.DataFrame({
'NAME': [storage['zagol']],
'PRICE': [storage['cena']],
'ARTICUL': [storage['articul']],
'IMG': [storage['img']]
})
df.to_excel(f'{file_name}.xlsx')
count += 1
据我了解,阿列克谢给你的建议是正确的。
我会(简而言之)做这样的事情:
df1 = pd.concat([df1, df2], ignore_index = True)https://www.geeksforgeeks.org/how-to-add-one-row-in-an-existing-pandas-dataframe/
附言。似乎追加已被弃用且不受支持。
将数据累积到列表中。完成所有处理后,创建一个数据帧并将其一次性写入文件: