Last active
October 15, 2019 17:18
-
-
Save dneprDroid/d748ecda4cda12c9a74dd7671597db73 to your computer and use it in GitHub Desktop.
Find Voter - check petition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| ###################### Параметры ######################### | |
| PETITION_ID = 73080 | |
| REQ_NAME = u'Овечко' | |
| START_PAGE = 1 | |
| END_PAGE = 10**100 | |
| ########################################################## | |
| import requests | |
| import json | |
| import re | |
| import time | |
| try: | |
| from HTMLParser import HTMLParser | |
| except ImportError: | |
| # Python 3 | |
| from html.parser import HTMLParser | |
| parser = HTMLParser() | |
| def decode_results(r): | |
| html_data = json.loads(r) | |
| table_html = html_data['table_html'] | |
| table_html_decoded = parser.unescape(table_html) | |
| voter_names = re.findall(r'(?<="table_cell name">)(.*)(?=<\/div>)', table_html_decoded) | |
| voter_dates = re.findall(r'(?<="table_cell date">)(.*)(?=<\/div>)', table_html_decoded) | |
| return map(lambda name, date: | |
| '%s : %s' % (parser.unescape(name), | |
| parser.unescape(date)), | |
| voter_names, voter_dates) | |
| def load_votes(petition_id, page_num): | |
| result = None | |
| while not result: | |
| try: | |
| result = requests.get('https://petition.president.gov.ua/petition/%s/votes/%s/json' % (petition_id, page_num), | |
| timeout=10) | |
| except Exception as e: | |
| print(e) | |
| if result.status_code == 404: | |
| return [], True | |
| try: | |
| return decode_results(result.content), False | |
| except ValueError: | |
| return [], True | |
| def main(): | |
| for i in range(END_PAGE - START_PAGE): | |
| page_num = START_PAGE + i | |
| print('Страница %s' % page_num) | |
| print('----------------------') | |
| parsed_voters, is_end = load_votes(PETITION_ID, page_num) | |
| if is_end: | |
| print('Не существует такой страницы : %s' % page_num) | |
| return | |
| for parsed_voter in parsed_voters: | |
| print(parsed_voter) | |
| if parsed_voter and REQ_NAME.lower() in parsed_voter.lower(): | |
| print('----------------------') | |
| print('Найдено имя: ') | |
| print(parsed_voter) | |
| return | |
| print('----------------------') | |
| time.sleep(0.3) # Don't DoS | |
| print('Ничего не найдено !!!') | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment