Given URL python how to get their content, and save it to HTML document.

Obtain the URL of the content need to use the standard library urllib package, which request module.

import urllib.request
url='http://www.baidu.com'
response=urllib.request.urlopen(url)
string=response.read()
html=string.decode('utf-8')
print(html)

the urlopen () method returns a <class 'http.client.HTTPResponse'>

I.e. Object Library Standard http bag, the package is a bag bottom, called by the request module.

read () method returns a <class 'bytes'>

Str target object into a byte by str.decode () method

Save the str objects acquired content to an HTML file, required to program the built-in method open ()

f=open('lc.html','w')
f.write(html)
f.close()

  open () method returns a <class '_io.TextIOWrapper'>

  write () method is to write the contents of the file object str

  Finally, close the file object

 

Guess you like

Origin www.cnblogs.com/blogzyq/p/11067648.html