я совсем запутался, у меня вылезают ошибки, т.к неправильно стоят отступы, как правильно их поставить?
from bs4 import BeautifulSoup
import requests
import json
def get_start(url):
headers ={
"accept" : "application/json",
"accept-encoding" : "gzip, deflate, br",
"accept-language": "ru,en-US;q=0.9,en;q=0.8,ru-RU;q=0.7",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
r = requests.get(url=url, headers= headers)
# with open("citilink4.html", "w") as file:
# file.write(r.text)
with open("citilink4.html") as file:
src = file.read()
r = requests.get(url=url, headers= headers)
soup = BeautifulSoup(r.text, "lxml")
cpu_link = soup.find_all("div", class_="ProductCardHorizontal__header-block")
try:
cpu_urls =[]
for cpu_cards in cpu_link[0:1]:
cpu_cards = cpu_cards.find("a")
cpu_cards = "https://www.citilink.ru" + cpu_cards.get("href")
cpu_urls.append(cpu_cards)
#print(cpu_cards)
#print(len(cpu_cards)) # check how many posischions we have
#print(cpu_urls)
except Exception:
cpu_cards = "Нету ссылки"
projects_data_list =[]
for cpu_cards in cpu_urls:
r = requests.get(cpu_cards, headers)
cpu_name = cpu_cards.split("/")[-2]
with open(f'all_citilink_pages/{cpu_name}', 'w') as file:
file.write(r.text)
with open(f'all_citilink_pages/{cpu_name}') as file:
src = file.read()
soup=BeautifulSoup(src, "lxml")
# #find_names = soup.find("div", class_="Container")
# # project_logo = soup.find("img", class_="__image-upper PreviewList__image Image").get("src") КАК МОЖНО НАЙТИ КАРТИНКУ
# # print(project_logo)
try:
find_cpu_name = soup.find("div", class_="ProductCardLayout__product-description").find("h1").text
#print(', '.join(f'{find_cpu_name}'.split(', ')[:2]).strip())
except Exception:
find_cpu_name = "Нету имени"
try:
find_price = soup.find("div",class_="ProductPrice ProductPrice_default ProductHeader__price-default").find("span", class_="ProductHeader__price-default_current-price").text
#print("Новая цена: " + find_price.strip())
except Exception:
find_price = "Нету цены"
try:
find_over_price = soup.find("div", class_="ProductHeader__price-block").find("span", class_="ProductHeader__price-old_current-price").text
#print("Старая цена: " + find_over_price.strip())
except Exception:
find_over_price = "Скидочки нету"
try:
find_stars = soup.find("div", class_="ProductHeader__info-item").find("span", class_="IconWithCount__count").text
#print("Средняя оценка: " + find_stars.strip())
except Exception:
find_stars = "Оценки нету"
projects_data_list.append(
{
"Название: " : find_cpu_name,
"Новая цена:": find_price,
"Старая цена: ": find_over_price,
"Средняя оценка: " : find_stars,
"Ссылка на сайт: " : cpu_link
}
)
print(projects_data_list)
# with open("result0.json", "w") as file:
# json.dump(projects_data_list, file, indent=4, ensure_ascii=False)
def main():
get_start("https://www.citilink.ru/catalog/processory/?pf=discount.any%2Crating.any%2Cms_action&f=discount.any%2Crating.any%2Cms_action&price_min=1990&price_max=20000")
if __name__ == "__main__":
main()