This program is written because the campus network bulletin board from time to time there will be academic reports, lectures and other information published, but this kind of information is often published in the day before the lecture, so that many important messages lost. The bulletin board also publishes some content that is irrelevant to the students, such as the meeting of the Chairman of the trade union.
The main difficulties encountered in the Chinese regular matching problem. (for example, through the first regular can be extracted to a page of all the Chinese title, the second regular from these Chinese titles will be able to match the "report" two words of the object added to the results list)
The school Announcements page is gb2312 encoded. The way I use it is that the entire project uses UTF-8 encoding, converting the keywords that need to be matched into the Utf-8 encoding format, and using the regular matching utf-8 encoding, which is explained in detail in the code.
Originally intended to put this on the public server on the daily push, access has been written well, has been unsuccessful access, finally found that the original server domain name to formally after the record can be, but I have to record this stubble son to forget. Back to the second, temporarily with 163 of the message SMS reminder function daily push.
#-*-coding:utf-8-*- fromBs4ImportBeautifulSoupImportUrllibImportQueueImportUrllib2ImportReImportOSImportSocketImportHttplibImportthreadingkeylist= [u'Report', u'Academic', u'Lectures', u'Auditorium', u'Employment', u'Project', u'Recruitment']keyresult=[]url='Http://urp.tust.edu.cn/bulletinPageList.jsp?pageNum=1&groupIds=Nyw4'req=Urllib2. Request (URL) res=Urllib2.urlopen (req) Soup=BeautifulSoup (Res.read ()) lists= Soup.select ('. An-list') strlists=the encoding of STR (lists) #此时列表里存储的是utf-8 is similar to u1234\\u2312\\u8473 means ' \ ' requires two \ \ to escape the character title= Re.findall (r'title= "(. *?)"', strlists) #匹配后也一样date= Re.findall (r'<p> (. *?) <', strlists) Herf= Re.findall (r'href= "(g.*?)"', strlists) forTinchTitle: forKeyinchKeylist:tempkey=Str (REPR (key)) #正则经常用到的repr函数 to see exactly what is represented inside python similar to u ' \u5c31\u4e1a ' Tempkey= Repr (Tempkey)#这一步把转义字符暴露出来, convenient match similar to U ' \\u5c31\\u4e1a 'Tempkey = Tempkey[3:tempkey.__len__()-2] #这一步把前面的 u ' and the back ' eraseifRe.search (R"'+tempkey+"', T): #开始匹配 keyresult.append (t) Break forKeyinchkeyresult:s="u '%s '"%(key) s=Eval (s)PrintS
Use Python regular match Chinese-crawl content of interest in Campus network bulletin boards