以前看了上文的帖子,感覺對自己有用,分享一下。
在這里我就細說一下步驟,給大家提供較完整的教程:本文對源代碼進行了刪減,源代碼功能更多,感興趣可以回去研讀一下
功能:
定時爬取課程,若有課程給自己發送郵件,用微信或者QQ郵箱給自己提醒
-
第一步:首先你要安裝python IDE
本文采用python3.7.0下載地址如下
鏈接:https://pan.baidu.com/s/1liQ4Z32kXo6secQpMYQw_w
提取碼:2qn3步驟:
1.解壓
2.以管理員身份運行python3.7.0- amd64
3.注意勾選Add python 3.7 to path,點擊Customize installation
4.點擊next
5.自己選擇安裝目錄
6.結束后關閉安裝程序,在開始菜單欄里找到IDLE python -
第二步:拷貝相應代碼並更改相應數據
1.打開IDLE
2.點擊新建窗口File-New File,將以下代碼復制進去
import requests, smtplib, email, time
from bs4 import BeautifulSoup as bs # 使用 BeautifulSoup庫對頁面進行解析
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
MAX = 18 # 作為周數的約束條件,最大值為21
INI = 125 # 作為訪問失敗的無效值,隨意定的
session = requests.Session()
# 登錄EPC
url_login = 'http://epc.ustc.edu.cn/n_left.asp'
data = {
'submit_type': 'user_login',
'name': '**********',
'pass': '**********',
'user_type': '2',
'Submit': 'LOG IN'
}
resp = session.post(url=url_login, data=data)
# 解析頁面,返回列表:[week,星期,教師,學時,上課時間,教室]
def getInfo(url):
resp = session.get(url)
resp.encoding = resp.apparent_encoding
# print(resp.text)
soup = bs(resp.text, 'html.parser')
tds = soup.select('td[align="center"]')
return [int(tds[14].string[1:3]), tds[15].string, ] + [string for string in tds[18].strings]
# tds[0] #只顯示可預約
# tds[1] #預約單元
# tds[2] #周數
# tds[3] #星期幾
# tds[4] #教師
# tds[5] #學時
# tds[6] #上課時間
# tds[7] #教室……
# tds[14] #第多少周
# tds[15].string #星期
# tds[16].string #教師
# [x for x in tds[18].strings] #時間
def getEPC():
# 返回數據:字典
# key:name or INI
# value:[week,星期,教師,學時,上課時間,教室] or [INI]
try:
url1 = 'http://epc.ustc.edu.cn/m_practice.asp?second_id=2001' # Situational dialogue
url2 = 'http://epc.ustc.edu.cn/m_practice.asp?second_id=2002' # Topical discussion
url3 = 'http://epc.ustc.edu.cn/m_practice.asp?second_id=2003' # Debate
url4 = 'http://epc.ustc.edu.cn/m_practice.asp?second_id=2004' # Drama
url7 = 'http://epc.ustc.edu.cn/m_practice.asp?second_id=2007' # Pronunciation Practice
info = {}
info['Situational Dialogue'] = getInfo(url1)
info['Topical Discussion'] = getInfo(url2)
info['Topical Discussion'] = getInfo(url2)
info['Debate'] = getInfo(url3)
info['Pronunciation Practice'] = getInfo(url7)
return info
except:
return {INI: [INI]}
# 郵箱發送
def Send_mail(text):
msg_from = '542127509@qq.com' # 發送方郵箱
passwd = '************' # 填入發送方郵箱的授權碼
msg_to = '648810974@qq.com' # 收件人郵箱
subject = "課程爬取" # 主題
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print('succeed')
except:
print("發送失敗")
finally:
s.quit()
# 主程序
while 1:
status = True
info = getEPC()
print(time.ctime(), ':')
for key, value in info.items():
print('{}:{}'.format(key, value))
print('\n')
for value in info.values():
if value[0] < MAX:
text = 'There is a course of {} in week{},{},{},{}'.format(key, value[0], value[1], value[2], value[3],end='\n\n')
print(text)
Send_mail(text)
else:
print('暫時沒有符合條件的課程')
time.sleep(60) # 這里修改刷新頻率
**
- 第三步:修改參數:
**
1.MAX = 18改成你想要的周數
2.data中紅色部分為自己賬號密碼:
3.圖片處加#號為不想要的課程,例如:只想選debate,把別的url都加上#,記得要對稱的加
4.以下四個與郵箱相關的地方地方更改
這里授權碼可以百度一下(非qq密碼),qq郵箱授權碼獲取如下:
https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001256&&id=28
5.最后一行刷新頻率根據自己需要改:
time.sleep(60) # 這里修改刷新頻率
**
- 第四步:運行腳本點擊Run-Run model
**
**
- 其他事項:
**
有的同學運行是發現如下提示,因為沒有安裝相應的requests庫函數,
解決步驟:
1.在win10下輸入框內輸入cmd並運行
2.安裝requests庫,輸入如下代碼,回車:
pip install requests
注釋:(若bs4庫沒安裝,與之類似,輸入 pip install bs4)
部分同學遇到如下情況,只需將匡內部分復制到命令框運行即可,重復上一步(無此現象忽略)
第四部:將QQ郵箱與微信綁定,關注公眾號或者下載QQ郵箱即可