[Python] web crawler: Bupt Library Rankings

Source: Internet
Author: User

Recently, the reptile is more interested in the study of a bit.

Recommend a Python crawler blog http://blog.csdn.net/pleasecallmewhy/article/details/9305229 Click to open the link. After the blogger's blog learned, he wrote a crawl Bupt library rankings of the demo.

#!/usr/bin/env python#-*-coding:utf-8-*-#---------------------------------------# Program: Bupt Library Crawler-Leaderboard # version: 0.1# Zhan gxuan# Date: 2015-04-15# language: Python 2.7# action: Enter account number and password # function: #---------------------------------------' a test module ' __aut hor__ = ' Zhangxuan ' import urllibimport urllib2import cookielibimport Recookie = cookielib. Cookiejar () opener = Urllib2.build_opener (urllib2. Httpcookieprocessor (cookie)) opener.addheaders = [(' User-agent ', ' mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) gecko/20100101 firefox/37.0 ')]urllib2.install_opener (opener) #需要POST的数据 #postdata=urllib.urlencode ({' Login_type ': ' E_card ', ' barcode ': ' 2014110590 ', ' Password ': ' 1105090 ', ' _ ': '} ' #自定义一个请求 #req = urllib2. Request (url = ' http://10.106.0.217:8080/opac_two/reader/infoList.jsp ', data = postdata) #访问该链接 # #result = Opener.open (req) result = Urllib2.urlopen (req) #打印返回的内容 #print result.read (). Decode (' GBK '). Encode (' Utf-8 ') #打印cookie的值for item in Cookie:print ' cookie:name = ' +item.name priNT ' Cookie:value = ' +item.valueresult = Opener.open (' http://10.106.0.217:8080/opac_two/top/top.jsp ') print U ""------ ------------------------------------------------------------------------"" "MyPage = Result.read () myitems = Re.findall (' <div class= '. *? >.*?<a href= ". *?". *?target= "_blank" >  (. *?) </a>.*?</div>.*?<div class= ". *?" > (. *?) </div> ', Mypage,re. S) for item in Myitems:print item[0].decode (' GBK '). Encode (' Utf-8 ') print item[1].decode (' GBK '). Encode (' Utf-8 ')
The code is basically modeled after that blog, but the specific analysis of the concrete part of the changes, we can go to see his detailed explanation.

Regular expressions fetch data because they do not have a lot of skill.



The number of times the title was taken out and the loan retrieved.

The final:



[Python] web crawler: Bupt Library Rankings

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.