First open the Campus network (take my school as an example "http://www.zhbit.com/")
Now we need to get the red box data and output
Below we open the browser development mode and navigate to the corresponding code
Not hard to find, school news only show 5 message
Now we're going to start writing programs.
#-*-Coding:utf-8-*-import urllibimport urllib2import reurl = "http://www.zhbit.com/" #利用urllib2模块打开校园网res = Urllib2.urlopen (URL) #把校园网的源代码存储到html变量当中html = Res.read () #根据相应的网页代码编写正则表达式匹配pattern = Re.compile (R ' <li><a Href=.*?title=.*?> (. *?) </a><span> (. *?) </span></li> ') #获取所有符合正则表达式的字符串h = Pattern.findall (HTML) #因为主页前5个符合正则表达式的内容刚好是我们需要的内容for I in range (5): #h I [0] Regular expression first (. *), h[i][1] for the second #输出h [i][0] (title) and H[i][1] (date) print h[i][0]+ "" +h[i][1]
Run results
Python crawler gets news on campus network