При нажатии «Просмотреть расписание» отправляется запрос с тем же URL-адресом, но с данными btnViewSched=View Schedule
и токены. Вот код, который собирает данные таблицы в формате списка карт:
import requests
from bs4 import BeautifulSoup
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/73.0.3683.86 Safari/537.36',
'DNT': '1',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,'
'application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'ru,en-US;q=0.9,en;q=0.8,tr;q=0.7',
}
response = requests.get('https://www.cnatra.navy.mil/scheds/schedule_data.aspx?sq=vt-9', headers=headers)
assert response.ok
page = BeautifulSoup(response.text, "lxml")
# get __VIEWSTATE, __EVENTVALIDATION and __VIEWSTATEGENERATOR for further requests
__VIEWSTATE = page.find("input", attrs={"id": "__VIEWSTATE"}).attrs["value"]
__EVENTVALIDATION = page.find("input", attrs={"id": "__EVENTVALIDATION"}).attrs["value"]
__VIEWSTATEGENERATOR = page.find("input", attrs={"id": "__VIEWSTATEGENERATOR"}).attrs["value"]
# View Schedule click set here
data = {
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATE': __VIEWSTATE,
'__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,
'__EVENTVALIDATION': __EVENTVALIDATION,
'btnViewSched': 'View Schedule',
'txtNameSearch': ''
}
# request with params
response = requests.post('https://www.cnatra.navy.mil/scheds/schedule_data.aspx?sq=vt-9', headers=headers, data=data)
assert response.ok
page = BeautifulSoup(response.text, "lxml")
# get table headers to map as a keys in result
table_headers = [td.text.strip() for td in page.select("#dgEvents tr:first-child td")]
# get all rows, without table headers
table_rows = page.select("#dgEvents tr:not(:first-child)")
result = []
for row in table_rows:
table_columns = row.find_all("td")
# use map with results for row and add all columns as map (key:value)
row_result = {}
for i in range(0, len(table_headers)):
row_result[table_headers[i]] = table_columns[i].text.strip()
# add row_result to result list
result.append(row_result)
for r in result:
print(r)
print("the end")
Пример вывода:
{'TYPE': 'Flight', 'VT': 'VT-9', 'Brief': '07: 45 ',' EDT ': '09: 45', 'RTB': '11: 15 ',' Инструктор ':' JARVIS, GRANT M [LT] ',' Student ':' LENNOX, KEVIN I [ENS] ',' Event ':' BI4101 ',' Hrs ':' 1.5 ',' Remarks ': '2 HR BRIEF MASS BRIEF', 'Местоположение': ''}