1 ImportUrllib22Response = Urllib2.urlopen ("http://www.baidu.com")3HTML =Response.read ()4 5 #EG26 ImportUrllib27req = Urllib2. Request ("http://www.baidu.com")8Response =urllib2.urllib2 (req)9The_page =Response.read ()Ten One #eg3 Post transfer data A ImportUrllib - ImportUrllib2 - theURL ="http://www.msdn.com" -values={'name':'Xu', - ' Location':'YJ', - 'language':'Python'} + -data =Urllib.urlencode (values) +req = Urllib2. Request (Url,data)#send a request while transmitting the data form AResponse = Urllib2.urlopen (req)#Receive Data atThe_page =Response.read () - - #EG4 Get Transfer data - #slightly - - #Eg5 Join User-agent in ImportUrllib - ImportUrllib2 to +URL ="http://www.msdn.com" -User_agent ='mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' thevalues={'name':'Xu', * ' Location':'YJ', $ 'language':'Python'}Panax Notoginseng -headers = {'user-agent': User_agent} thedata =Urllib.urlencode (values) +req = Urllib2. Request (Url,data,headers)#send requests while transmitting data forms and user-agent AResponse = Urllib2.urlopen (req)#Receive Data theThe_page =Response.read () + - #Eg6 Catching exceptions $ Try: $Response = Urllib2.urlopen (req)#Receive Data - exceptUrllib2. Urlerror,e: - PrintE.reason the PrintE.code#404 or ... - #Way2Wuyi Try: theResponse = Urllib2.urlopen (req)#Receive Data - exceptUrllib2. Httperror,e: Wu PrintE.code#404 or ... - exceptUrllib2. Urlerror,e: About PrintE.reason $ - #way3. We command to handle exception - Try: -Response = Urllib2.urlopen (req)#Receive Data A exceptUrllib2. Urlerror,e: + ifHasattr (E,'Code'): the Print 'Error Code:', E.code - elifHasattr (E,'reason'): $ Print 'Reason:', E.reason the the #Eg7 the fromUrllib2ImportRequest,urlopen,urlerror,httperror theOld_url ="http://www.baidu.com" -req =Request (Old_url) inResponse =Urlopen (req) theRel_url =Response.geturl () theinfo =Response.info () About the #Eg8 Cookies the ImportUrllib2 the ImportCookielib +Cookie =Cookielib. Cookiejar () -Opener =Urllib2.build_opener (urllib2. Httpcookieprocessor (cookie)) theResponse = Opener.open ("http://www.baidu.com")Bayi forIteminchCookies: the PrintItem.name,item. the - #eg9 Regular Expressions - ImportRe thePattern = Re.compile (r"Hello") theMatch1 = Pattern.match ("Hello World") the ifMatch1: the PrintMatch1.group () - Else: the Print "Match failed"
Python crawler notes