Python crawler notes

Source: Internet
Author: User

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

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.