Python crawler gets news on campus network

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.