爬虫访问百度网站
from urllib.request import ProxyHandler, build_opener
from urllib.error import URLError
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
proxy_handler = ProxyHandler({
'http': 'http://101.200.235.69:8080',
'https': 'https://101.200.235.69:8080'
})
headers = ("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
opener = build_opener(proxy_handler)
opener.addheaders = [headers]
try:
response = opener.open('https://www.baidu.com') # 用opener类中open方法打开链接
print(response.read().decode('utf-8'))
except URLError as e:
# 定义异常实例(except IOError as e)
print(e.reason)
结果