SHOW:
|
|
- or go back to the newest paste.
| 1 | from time import sleep | |
| 2 | ||
| 3 | import requests | |
| 4 | from bs4 import BeautifulSoup | |
| 5 | from openpyxl import Workbook | |
| 6 | ||
| 7 | ||
| 8 | # Функция для парсинга страницы и сохранения данных в Excel | |
| 9 | def parse_page(next_response, ws): | |
| 10 | soup = BeautifulSoup(next_response.content, 'html.parser') | |
| 11 | table = soup.find('table')
| |
| 12 | ||
| 13 | - | try: |
| 13 | + | rows = table.findAll('tr')
|
| 14 | - | rows = table.findAll('tr')
|
| 14 | + | |
| 15 | - | except AttributeError as e: |
| 15 | + | |
| 16 | - | print(f'\n{next_response.url} - {next_response.status_code}\n{e}')
|
| 16 | + | |
| 17 | - | return -1 |
| 17 | + | |
| 18 | for cell in cells: | |
| 19 | data.append(cell.text.strip()) | |
| 20 | ws.append(data) | |
| 21 | ||
| 22 | ||
| 23 | def main(): | |
| 24 | session = requests.Session() | |
| 25 | ||
| 26 | # Создаем объект Workbook для создания Excel файла | |
| 27 | wb = Workbook() | |
| 28 | # Активируем лист | |
| 29 | ws = wb.active | |
| 30 | ||
| 31 | url = 'https://www.rst.gov.ru/portal/gost/home/activity/compliance/evaluationcompliance/AcknowledgementCorrespondence/safetycertificate018?portal:componentId=ff119059-8bd4-47fc-95f6-a70de17a4b3e&portal:isSecure=false&portal:portletMode=view&navigationalstate=JBPNS_rO0ABXdgAAhwYWdlU2l6ZQAAAAEAAjIwAAdvcmRlckJ5AAAAAQAYZGF0ZW9maXNzdWVvZmNlcnRpZmljYXRlAARmcm9tAAAAAQABMAAFb3JkZXIAAAABAARERVNDAAdfX0VPRl9f' | |
| 32 | response = session.get(url) | |
| 33 | soup = BeautifulSoup(response.content, 'html.parser') | |
| 34 | parse_page(response, ws) | |
| 35 | ||
| 36 | ccc = 0 | |
| 37 | # Парсинг остальных страниц с помощью кнопки "вперед" | |
| 38 | while ccc <= 10000: | |
| 39 | next_button = soup.find('a', string='Вперед →')
| |
| 40 | if next_button: | |
| 41 | next_page_url = next_button['href'] | |
| 42 | next_response = session.get(next_page_url) | |
| 43 | ||
| 44 | sleep(0.6) | |
| 45 | try: | |
| 46 | parse_page(next_response, ws) | |
| 47 | - | soup = BeautifulSoup(response.content, 'html.parser') |
| 47 | + | except Exception as e: |
| 48 | print(f'{next_response.url} - {next_response.status_code}\n{e}')
| |
| 49 | - | parse_page(next_response, ws) |
| 49 | + | soup = BeautifulSoup(next_response.content, 'html.parser') |
| 50 | ||
| 51 | ccc += 1 | |
| 52 | print(ccc) | |
| 53 | else: | |
| 54 | break | |
| 55 | ||
| 56 | # Сохраняем Excel файл | |
| 57 | wb.save('output.xlsx')
| |
| 58 | ||
| 59 | ||
| 60 | if __name__ == '__main__': | |
| 61 | main() | |
| 62 |