python入门六

1.模拟网页请求

import urllib2
import urllib
#打开日志
url = 'https://www.zhihu.com/'
httpHandler = urllib2.HTTPHandler(debuglevel=1)
httpsHandler = urllib2.HTTPSHandler(debuglevel=1)
opener = urllib2.build_opener(httpHandler, httpsHandler)
urllib2.install_opener(opener)
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}

request = urllib2.Request(url, None, headers)
response = urllib2.urlopen(request)
page = response.read()

print page

你可能感兴趣的:(python入门六)