Вы можете использовать sheet.append()
для добавления строк в файл Excel.
Например:
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
print('Welcome!')
search = input('Search for what? ')
URL = 'https://www.merinfo.se/search?ap=1&emp=0%3A100&rev=0%3A100000&d=c&who=' + search + '&where=&bf=1'
headers = {'User Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
workbook = Workbook()
sheet = workbook.active
for i in soup.find_all('h2', class_='name'):
txt = i.get_text(strip=True)
print(txt)
sheet.append([txt])
workbook.save(filename="foretagen.xlsx")
Выводит:
Welcome!
Search for what? tree
Tree Logistics AB
Sushi tree AB
Beech Tree AB
BIN TREE AB
TALENT TREE AB
Juniper Tree AB
Hope Tree AB
Dead tree group AB
The Tree Karsikko AB
Willow Tree Consulting AB
Learning Tree International Aktiebolag
Palm Tree Tunes AB
Tree of Pets AB
nordic tree care AB
For Tree i Rossön Aktiebolag
Trädkompaniet Voice Of Tree Care AB
Omsorgshuset Red Tree Care Center AB
Global Tree Care i Sundbyberg AB
Three Rock AB
Three Gates AB
и записывает файл excel ( снимок экрана из LibreOffice):
введите описание изображения здесь