美文网首页
response网络详细信息

response网络详细信息

作者: 去角落里种蘑菇 | 来源:发表于2018-11-23 11:15 被阅读0次

视频地址 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网络详细信息

      本文链接:https://www.haomeiwen.com/subject/qaodqqtx.html