Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cards as cards
- import requests
- from bs4 import BeautifulSoup
- import csv
- CSV = 'cards.csv'
- HOST = 'http://alegeri.md/'
- URL = 'http://alegeri.md/w/Pre%C8%99edin%C8%9Bii_raioanelor_din_Republica_Moldova'
- HEADERS = {
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
- 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
- }
- def get_html(url, params=''):
- r = requests.get(url, headers=HEADERS)
- return r
- def get_content(html):
- soup = BeautifulSoup(html, 'html.parser')
- items = soup.find_all('div', class_='table table-condensed table-striped deputees sortable vmid jquery-tablesorter')
- cards = []
- for item in items:
- cards.append(
- {
- 'Citi': item.find('td data-sort-value', 'a').find('a'),
- 'Photo': HOST + item.find('div', class_='rphoto').find('img').get('src'),
- 'First_Name_Last_Name': item.find('td data-sort-value').find('a').get('href'),
- 'The_consignment': item.find('td', class_='grey').get_text(strip=True)
- }
- )
- return cards
- def save_doc(items, path):
- with open(path, 'w', newline='') as file:
- writer = csv.writer(file, delimiter=';')
- writer.writerow(['Город', 'Фото', 'Фамилия Имя', 'Партия'])
- for item in items:
- writer.writerow([item['Citi'], item['Photo'], item['First_Name_Last_Name'], item['The_consignment']])
- def parser():
- html = get_html(URL)
- if html.status_code == 200:
- cards = []
- for page in range(1):
- print(f'Парсим стр.: {page}')
- html = get_html(URL) # 2argument: params={'page': page}
- cards.extend(get_content(html.text))
- save_doc(cards, CSV)
- else:
- print('Error')
- parser()
Advertisement
Add Comment
Please, Sign In to add comment