Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- import requests
- from bs4 import BeautifulSoup
- from openpyxl import Workbook
- # Функция для парсинга страницы и сохранения данных в Excel
- def parse_page(next_response, ws):
- soup = BeautifulSoup(next_response.content, 'html.parser')
- table = soup.find('table')
- try:
- rows = table.findAll('tr')
- except AttributeError as e:
- print(f'\n{next_response.url} - {next_response.status_code}\n{e}')
- return -1
- for row in rows:
- cells = row.findAll('td')
- data = []
- for cell in cells:
- data.append(cell.text.strip())
- ws.append(data)
- def main():
- session = requests.Session()
- # Создаем объект Workbook для создания Excel файла
- wb = Workbook()
- # Активируем лист
- ws = wb.active
- 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'
- response = session.get(url)
- soup = BeautifulSoup(response.content, 'html.parser')
- parse_page(response, ws)
- ccc = 0
- # Парсинг остальных страниц с помощью кнопки "вперед"
- while ccc <= 10000:
- next_button = soup.find('a', string='Вперед →')
- if next_button:
- next_page_url = next_button['href']
- next_response = session.get(next_page_url)
- soup = BeautifulSoup(response.content, 'html.parser')
- sleep(0.6)
- parse_page(next_response, ws)
- ccc += 1
- print(ccc)
- else:
- break
- # Сохраняем Excel файл
- wb.save('output.xlsx')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment