python中使用urllib2获取http请求状态码的代码例子

yipeiwu_com6年前Python基础

采集内容常需要得到网页返回的验证码做进一步处理

下面代码是用python写的用来获取网页http状态码的脚本

#!/usr/bin/python
# -*- coding: utf-8 -*-
#encoding=utf-8
#Filename:states_code.py
 
import urllib2
 
url = '//www.jb51.net/'
response = None
try:
  response = urllib2.urlopen(url,timeout=5)
except urllib2.URLError as e:
  if hasattr(e, 'code'):
    print 'Error code:',e.code
  elif hasattr(e, 'reason'):
    print 'Reason:',e.reason
finally:
  if response:
    response.close()

相关文章

浅谈Python数据类型之间的转换

Python数据类型之间的转换 函数 描述 int(x [,base])...

Pandas0.25来了千万别错过这10大好用的新功能

Pandas0.25来了千万别错过这10大好用的新功能

呆鸟云:“7 月 18 日,Pandas 团队推出了 Pandas 0.25 版,这就相当于 Python 3.8 啦,Python 数据分析师可别错过新版的好功能哦。” 安装 0.2...

浅谈Python中的私有变量

私有变量表示方法 在变量前加上两个下划线的是私有变量。 class Teacher(): def __init__(self,name,level): self.__na...

详谈python3中用for循环删除列表中元素的坑

for循环语句的对象是可迭代对象,可迭代对象需要实现__iter__或iter方法,并返回一个迭代器,什么是迭代器呢?迭代器只需要实现 __next__或next方法。 现在来验证一下列...

Python 条件判断的缩写方法

return (1==1) ? "is easy" : "my god" //C...