打印出空闲时间并保存在字典中
Print out times that are free and save them in dictionary
我正在研究时间table 系统并且需要所有空闲时间(学生没有讲座)。现在它打印出整个时间table。我只需要将所有空闲插槽存储在某个地方。它们在时间 table 上显示为“NaN”。这是我的代码。
from bs4 import BeautifulSoup
import pandas as pd
import requests
import time
import natsort as ns
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window() #opens up website, probably not needed.
driver.implicitly_wait(30)
driver.get('https://opentimetable.dcu.ie/')
select = Select(driver.find_element_by_tag_name("select"))
select.select_by_visible_text("Programmes of Study")
search = driver.find_element_by_id("textSearch")
search.send_keys("CASE2")
checkbox = driver.find_element_by_xpath('.//input[following-sibling::div[contains(text(), "CASE2")]]') # it works but it is harder to remeber
checkbox.click()
time.sleep(3)
html = driver.find_element_by_id("week-pdf-content").get_attribute('outerHTML')
df2 = pd.read_html(html)[0]
#trying to print free slots
x = 0
if df2[x] == "NaN":
print(df[x])
x+=1
print(df2.to_string()) # to_string() to display all columns without `...`
预期输出将是所有空闲时间打印出来的。例如星期一:11-12、13-14、16-17。每一天。
这是当前输出
如您所见,它只是打印出时间table。我想把所有的时间都存储在它说 NaN
的地方
注意: 这只是一时冲动适应,以后应该可以自行微调
根据你的更新,我现在知道输出应该是什么了。为了简单地以类似的结构输出信息,如果您设置了保留时间的第一列作为索引,则可以遍历数据帧的每一列。
df = df2.set_index('Unnamed: 0') #or df = df2.set_index(list(df2.columns[[0]]))
for column in df:
print(f'{column}:{", ".join(df[df[column].isna()].index.drop_duplicates().to_list())}')
输出
Mon:8:00, 9:00, 13:00, 18:00, 19:00, 20:00, 21:00
Tue:8:00, 13:00, 14:00, 15:00, 17:00, 18:00, 19:00, 20:00, 21:00
Wed:8:00, 13:00, 15:00, 17:00, 18:00, 19:00, 20:00, 21:00
Thu:8:00, 11:00, 13:00, 14:00, 15:00, 18:00, 19:00, 20:00, 21:00
Fri:8:00, 9:00, 10:00, 11:00, 12:00, 13:00, 14:00, 15:00, 16:00, 17:00, 18:00, 19:00, 20:00, 21:00
将这些信息存储在字典中:
data = []
for column in df:
data.append({column:df[df[column].isna()].index.drop_duplicates().to_list()})
输出
[{'Mon': ['8:00', '9:00', '13:00', '18:00', '19:00', '20:00', '21:00']}, {'Tue': ['8:00', '13:00', '14:00', '15:00', '17:00', '18:00', '19:00', '20:00', '21:00']}, {'Wed': ['8:00', '13:00', '15:00', '17:00', '18:00', '19:00', '20:00', '21:00']}, {'Thu': ['8:00', '11:00', '13:00', '14:00', '15:00', '18:00', '19:00', '20:00', '21:00']}, {'Fri': ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00']}]
我正在研究时间table 系统并且需要所有空闲时间(学生没有讲座)。现在它打印出整个时间table。我只需要将所有空闲插槽存储在某个地方。它们在时间 table 上显示为“NaN”。这是我的代码。
from bs4 import BeautifulSoup
import pandas as pd
import requests
import time
import natsort as ns
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window() #opens up website, probably not needed.
driver.implicitly_wait(30)
driver.get('https://opentimetable.dcu.ie/')
select = Select(driver.find_element_by_tag_name("select"))
select.select_by_visible_text("Programmes of Study")
search = driver.find_element_by_id("textSearch")
search.send_keys("CASE2")
checkbox = driver.find_element_by_xpath('.//input[following-sibling::div[contains(text(), "CASE2")]]') # it works but it is harder to remeber
checkbox.click()
time.sleep(3)
html = driver.find_element_by_id("week-pdf-content").get_attribute('outerHTML')
df2 = pd.read_html(html)[0]
#trying to print free slots
x = 0
if df2[x] == "NaN":
print(df[x])
x+=1
print(df2.to_string()) # to_string() to display all columns without `...`
预期输出将是所有空闲时间打印出来的。例如星期一:11-12、13-14、16-17。每一天。
这是当前输出 如您所见,它只是打印出时间table。我想把所有的时间都存储在它说 NaN
的地方注意: 这只是一时冲动适应,以后应该可以自行微调
根据你的更新,我现在知道输出应该是什么了。为了简单地以类似的结构输出信息,如果您设置了保留时间的第一列作为索引,则可以遍历数据帧的每一列。
df = df2.set_index('Unnamed: 0') #or df = df2.set_index(list(df2.columns[[0]]))
for column in df:
print(f'{column}:{", ".join(df[df[column].isna()].index.drop_duplicates().to_list())}')
输出
Mon:8:00, 9:00, 13:00, 18:00, 19:00, 20:00, 21:00
Tue:8:00, 13:00, 14:00, 15:00, 17:00, 18:00, 19:00, 20:00, 21:00
Wed:8:00, 13:00, 15:00, 17:00, 18:00, 19:00, 20:00, 21:00
Thu:8:00, 11:00, 13:00, 14:00, 15:00, 18:00, 19:00, 20:00, 21:00
Fri:8:00, 9:00, 10:00, 11:00, 12:00, 13:00, 14:00, 15:00, 16:00, 17:00, 18:00, 19:00, 20:00, 21:00
将这些信息存储在字典中:
data = []
for column in df:
data.append({column:df[df[column].isna()].index.drop_duplicates().to_list()})
输出
[{'Mon': ['8:00', '9:00', '13:00', '18:00', '19:00', '20:00', '21:00']}, {'Tue': ['8:00', '13:00', '14:00', '15:00', '17:00', '18:00', '19:00', '20:00', '21:00']}, {'Wed': ['8:00', '13:00', '15:00', '17:00', '18:00', '19:00', '20:00', '21:00']}, {'Thu': ['8:00', '11:00', '13:00', '14:00', '15:00', '18:00', '19:00', '20:00', '21:00']}, {'Fri': ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00']}]