使用python SSL下载google.com.au页面

时间:2015-06-08 09:01:27

标签: python ssl

以下代码能够检索安全网站主页的源代码,例如www.stgeorge.com.au

import socket, ssl
context = ssl.create_default_context()
context.verify_mode = ssl.CERT_OPTIONAL
context.check_hostname = True
context.load_default_certs()
host = 'www.stgeorge.com.au'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = context.wrap_socket(s, server_hostname=host) 
ssl_sock.connect((host, 443)) 
ssl_sock.send('GET https://' + host + ' HTTP/1.0\n\n')

while True:
    data = ssl_sock.recv(1024)
    if not data:
        break
    print data
ssl_sock.close()

但是,如果我用www.google.com.au替换www.stgeorge.com.au,pyton报告错误“SSL:CERTIFICATE_VERIFY_FAILED”

从google.com.au获取源代码需要做什么?

此致 尼克

1 个答案:

答案 0 :(得分:0)

使用请求库后,如skyline75489所示,代码如下所示:

import requests
r = requests.get('https://www.google.com.au')
f = open('test.txt', 'w')
f.write((r.text).encode('utf8'))
f.close()
print((r.text).encode('utf8'))

感谢您的帮助