@No_FaP

Добрый вечер, пытаюсь запустить парсер сайта, выдает ошибки, в чем проблема?


  • Unexpected indent
    Statement expected, found Py:DEDENT
    Variable in function should be lowercase
    Variable in function should be lowercase
    Global variable 'comp' is undefined at the module level
    Typo: In word 'KHTML'
    Typo: In word 'marginright'
    Typo: In word 'marginright'
from bs4 import BeautifulSoup
import requests

def save():
	with open('parse_info.txt', 'a') as file:
		file.write(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}\n')

def parse():
	URL = 'https://www.olx.kz/transport/legkovye-avtomobili/'
	HEADERS = {
		'User-Agent': '&&&'
	}

	response = requests.get(URL, headers = HEADERS)
	soup = BeautifulSoup(response.content, 'html.parser')
	items = soup.findAll('div', class_ = 'offer-wrapper')
	comps = []

	for item in items:
		comps.append({
			'title': item.find('a', class_ = 'marginright5 link linkWithHash detailsLink linkWithHashPromoted').get_text(strip = True),
			'price': item.find('p', class_ = 'price').get_text(strip = True),
			'link': item.find('a', class_ = 'marginright5 link linkWithHash detailsLink linkWithHashPromoted').get('href')
			})

			global comp
			for comp in comps:
				print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}')
				save()


  • Вопрос задан
  • 1136 просмотров
Пригласить эксперта
Ответы на вопрос 2
SoreMix
@SoreMix Куратор тега Python
yellow
Лишие пробелы перед global и for за ней
Ответ написан
Комментировать
@Arm0x
from bs4 import BeautifulSoup
import requests


def save():
	with open('parse_info.txt', 'a') as file:
		file.write(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}\n')


def parse():
	URL = 'https://www.olx.kz/transport/legkovye-avtomobili/'
	HEADERS = {
		'User-Agent': '&&&'
	}

	response = requests.get(URL, headers=HEADERS)
	soup = BeautifulSoup(response.content, 'html.parser')
	items = soup.findAll('div', class_='offer-wrapper')
	comps = []

	for item in items:
		comps.append({
			'title': item.find('a', class_='marginright5 link linkWithHash detailsLink linkWithHashPromoted').get_text(
			strip=True),
			'price': item.find('p', class_='price').get_text(strip=True),
			'link': item.find('a', class_='marginright5 link linkWithHash detailsLink linkWithHashPromoted').get('href')
		})

	global comp
	for comp in comps:
		print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}')
		save()
parse()
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
07 мая 2024, в 14:11
4000 руб./за проект
07 мая 2024, в 14:10
10000 руб./за проект
07 мая 2024, в 14:04
1300 руб./за проект