Guest User

print("Парсинг")

a guest
Dec 11th, 2020
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import cards as cards
  2. import requests
  3. from bs4 import BeautifulSoup
  4. import csv
  5.  
  6. CSV = 'cards.csv'
  7. HOST = 'http://alegeri.md/'
  8. URL = 'http://alegeri.md/w/Pre%C8%99edin%C8%9Bii_raioanelor_din_Republica_Moldova'
  9. HEADERS = {
  10. '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',
  11. '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'
  12. }
  13.  
  14.  
  15. def get_html(url, params=''):
  16. r = requests.get(url, headers=HEADERS)
  17. return r
  18.  
  19.  
  20. def get_content(html):
  21. soup = BeautifulSoup(html, 'html.parser')
  22. items = soup.find_all('div', class_='table table-condensed table-striped deputees sortable vmid jquery-tablesorter')
  23. cards = []
  24.  
  25. for item in items:
  26. cards.append(
  27. {
  28. 'Citi': item.find('td data-sort-value', 'a').find('a'),
  29. 'Photo': HOST + item.find('div', class_='rphoto').find('img').get('src'),
  30. 'First_Name_Last_Name': item.find('td data-sort-value').find('a').get('href'),
  31. 'The_consignment': item.find('td', class_='grey').get_text(strip=True)
  32.  
  33. }
  34. )
  35. return cards
  36.  
  37.  
  38. def save_doc(items, path):
  39. with open(path, 'w', newline='') as file:
  40. writer = csv.writer(file, delimiter=';')
  41. writer.writerow(['Город', 'Фото', 'Фамилия Имя', 'Партия'])
  42. for item in items:
  43. writer.writerow([item['Citi'], item['Photo'], item['First_Name_Last_Name'], item['The_consignment']])
  44.  
  45.  
  46. def parser():
  47. html = get_html(URL)
  48. if html.status_code == 200:
  49. cards = []
  50. for page in range(1):
  51. print(f'Парсим стр.: {page}')
  52. html = get_html(URL) # 2argument: params={'page': page}
  53. cards.extend(get_content(html.text))
  54. save_doc(cards, CSV)
  55. else:
  56. print('Error')
  57.  
  58.  
  59. parser()
Advertisement
Add Comment
Please, Sign In to add comment