response网络详细信息

视频地址 https://www.bilibili.com/video/av31369828/?p=9

#py3
import urllib.request
#pycharm go declaration to search source code
def download(url):
  response = urllib.request.urlopen(url, timeout = 5) 

print(type(response))# class http.client.httpresponse
print(response.info())
print(download("http://ww.baidu.com"))

代码升级的问题

#py2
#coding:utf-8
import urllib2


def download(url):
  response = urllib2.urlopen(url, timeout = 5) 
  print(type(response))# class http.client.httpresponse
  print(response.info()) #包含了网站的详细信息
  print(response.read()) #read source coad

#括号内是控制多少字符的问题
#写爬虫记得try catch
try:
  print(download("http://ww.google.com"))
except urllib2.URLError as e:
  print("网络异常", e) #抓住错误对象类型当作变量

你可能感兴趣的:(response网络详细信息)