Advertisement
Mirzali

Untitled

Jan 28th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. #pip install requests
  4. #pip install beautifulsoup4
  5.  
  6.  
  7. URL= 'https://auto.ria.com/newauto/marka-hyundai/'
  8.  
  9. HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
  10.            'Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363', 'accept': '*/*'}
  11. HOST = 'https://auto.ria.com'
  12.  
  13. def get_html(url, params=None):
  14.     r = requests.get(url, headers=HEADERS, params=params)
  15.     return  r
  16.  
  17.  
  18. def get_content(html):
  19.     soup = BeautifulSoup(html, 'html.parser')
  20.     items = soup.find_all('a', class_='na-card-item')
  21.  
  22.     cars =[]
  23.     for item in items:
  24.         uah_price = item.find('span', class_='size15')
  25.         if uah_price:
  26.             uah_price = uah_price.get_text().replace('•', '')
  27.         else:
  28.             uah_price = 'Цены нет'
  29.         cars.append({
  30.             'title': item.find('div', class_='na-card-name').get_text(strip=True),
  31.             'link': HOST + item.find('span', class_='link').get('href'),
  32.             'uah_price': uah_price,
  33.             'link': item.find('svg', class_='svg_i16_pin').find_next('span').get_text(),
  34.  
  35.             #    class ="na-card-price"
  36.         })
  37.     return cars
  38.  
  39.  
  40. def parse():
  41.     html = get_html(URL)
  42.     if html.status_code == 200:
  43.         cars = get_content(html.text)
  44.  
  45.     else:
  46.         print("Error")
  47.  
  48. parse()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement