Не выводятся данные, выводит только Process finished with exit code 0. Python
Python. Не выводятся данные, выводит только Process finished with exit code 0
import requests
from bs4 import BeautifulSoup
URL = 'https://www.avito.ru/schelkovo/avtomobili/audi/a6-ASgBAgICAkTgtg3elyjitg3onSg?cd=1&radius=200'
HEADERS = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8'}
HOST = 'https://www.avito.ru/'
def get_html(url, params=''):
r = requests.get(url)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', class_='iva-item-root-Nj_hb')
cars = []
for item in items:
cars.append({
'title': item.find('div', class_='iva-item-titleStep-_CxvN').find_next('a').find_next('h3').get_text(strip=True),
'link': item.find('div', class_='iva-item-titleStep-_CxvN').find_next('a').get('href'),
'price': item.find('span', class_= 'price-text-E1Y7h').get_text(),
'facts': item.find('div', class_= 'iva-item-text-_s_vh').get_text(),
'city': item.find('div', class_= 'geo-georeferences-Yd_m5').find_next('span').get_text(),
})
return cars
def parse():
html = get_html(URL)
#print(html.content) #непосредственно всё, что находится на странице
#print(html.headers.get('content-type')) #узнать то, в каком виде зашифрована информация
if html.status_code == 200:
cars = get_content(html.text)
return cars
else:
print('Error')
parse()
Ответы (1 шт):
Автор решения: Roman Trapeznikov
→ Ссылка
В строке с return cars вы просто возвращаете значение, которое потом не используете (в последней строке parse()). Попробуйте заменить return cars на print(cars) или в последней строке parse() на print(parse()):
import requests
from bs4 import BeautifulSoup
URL = 'https://www.avito.ru/schelkovo/avtomobili/audi/a6-ASgBAgICAkTgtg3elyjitg3onSg?cd=1&radius=200'
HEADERS = {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8'
}
HOST = 'https://www.avito.ru/'
def get_html(url, params=''):
r = requests.get(url)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', class_='iva-item-root-Nj_hb')
cars = []
for item in items:
cars.append({
'title':
item.find('div', class_='iva-item-titleStep-_CxvN').find_next(
'a').find_next('h3').get_text(strip=True),
'link':
item.find(
'div',
class_='iva-item-titleStep-_CxvN').find_next('a').get('href'),
'price':
item.find('span', class_='price-text-E1Y7h').get_text(),
'facts':
item.find('div', class_='iva-item-text-_s_vh').get_text(),
'city':
item.find(
'div',
class_='geo-georeferences-Yd_m5').find_next('span').get_text(),
})
return cars
def parse():
html = get_html(URL)
#print(html.content) #непосредственно всё, что находится на странице
#print(html.headers.get('content-type')) #узнать то, в каком виде зашифрована информация
if html.status_code == 200:
cars = get_content(html.text)
return cars
else:
print('Error')
print(parse())