from bs4 import BeautifulSoup
import requests
import openpyxl
book = openpyxl.Workbook()
sheet = book.active
def save():
global ill
sheet.cell(row=ill, column=1).value = comp["title"]
sheet.cell(row=ill, column=2).value = comp["price"]
sheet.cell(row=ill, column=3).value = comp["link"]
ill += 1
def parse():
URL = 'https://www.olx.pl/motoryzacja/samochody/'
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
response = requests.get(URL, headers=HEADERS)
soup = BeautifulSoup(response.content, 'html.parser')
items = soup.findAll('div', class_='offer-wrapper')
comps = []
for item in items:
titles = item.find('a', class_=['marginright5 link linkWithHash detailsLinkPromoted linkWithHashPromoted',
'marginright5 link linkWithHash detailsLink'])
if titles != None:
title = titles.get_text(strip=True)
else:
continue
price = item.find('p', class_='price').get_text(strip=True)
links = item.find('a', class_=['marginright5 link linkWithHash detailsLinkPromoted linkWithHashPromoted',
'marginright5 link linkWithHash detailsLink'])
if links != None:
link = links.get('href')
else:
continue
comps.append({
'title': title,
'price': price,
'link': link
})
global comp
for comp in comps:
print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}')
save()
ill = 2
sheet.cell(row=1, column=1).value = "Title"
sheet.cell(row=1, column=2).value = "Price"
sheet.cell(row=1, column=3).value = "Link"
parse()
book.save('result.xlsx')
book.close()
print(ill)
Я написал вот такой код. Он выводит в таблицу ссылку, название и цену объявления с определенной страницы фильтра. Как внедрить в код парсинг номера телефона с olx?