Five ways to write simple reptiles in Python

Source: Internet
Author: User
Tags in python

Ways to get HTML "one": Using Urllib
#-*-Coding:utf-8-*-

Import Urllib

 

' get Web page content and return '

def getwebpagecontent (URL):

f = urllib.urlopen (URL)

data = F.read ()

f.close ()

Return Data

url = ' http://blog.csdn.net '

content = getwebpagecontent (URL)

Print Content


Ways to get HTML "two": Using Pycurl

# Pycurl Reference Address: http://pycurl.sourceforge.net/

# pycurl Download Address: http://pycurl.sourceforge.net/download/pycurl-7.18.1.tar.gz

#-*-coding:utf-8-*-

Importpycurl

Importstringio

 

defgeturlcontent_pycurl (URL):

C = Pycurl. Curl ()

c.setopt (Pycurl. Url,url)

B = Stringio.stringio ()

c.setopt (Pycurl. Writefunction, B.write)

c.setopt (Pycurl. Followlocation, 1)

c.setopt (Pycurl. Maxredirs, 5)

# Agent

#c. setopt (Pycurl. PROXY, ' http://11.11.11.11:8080 ')

#c. setopt (Pycurl. Proxyuserpwd, ' aaa:aaa ')

C.perform ()

Returnb.getvalue ()

 

url = ' http://blog.csdn.net '

content =geturlcontent_pycurl (URL)

Print Content


Ways to get HTML "three": Using Cpamie

# Cpamie Download: http://sourceforge.net/project/showfiles.php?group_id=103662

#-*-coding:utf-8-*-

Import Cpamie

defgeturlcontent_cpamie (URL):

g_ie =cpamie.pamie ()

g_ie.showdebugging = False

g_ie.framename= None

g_ie.navigate (URL)

content =g_ie.pagegettext ()

g_ie.quit ()

returncontent

 

url = ' http://blog.csdn.net '

content = Geturlcontent_cpamie (URL)

Print Content


Ways to get HTML "four": Download files using Urllib

#-*-Coding:utf-8-*-

Import Urllib

 

url = ' http://blog.csdn.net '

path = ' c://temp//csdn.net.html '

Urllib.urlretrieve (Url,path)


Ways to get HTML "four": using the twisted framework of Client.getpage

# Twisted Frame Download:

Http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted_NoDocs-8.1.0.win32-py2.5.exe

#-*-coding:utf-8-*-

fromtwisted.internet Import Reactor

fromtwisted.web Import Client

 

Defresult (content):

Print Content

reactor.stop ()

 

deferred =client.getpage ("Http://blog.csdn.net")

deferred.addcallback (Result)

Reactor.run ()

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.