python获取网页状态码示例

yipeiwu_com6年前Python基础

代码很简单,只需要2行代码就可实现想要的功能,虽然很短,但确实使用,主要使用了requests库。

测试2XX, 3XX, 4XX, 5XX都能准确识别。

复制代码 代码如下:

#coding=utf-8

import requests

def getStatusCode(url):
 r = requests.get(url, allow_redirects = False)
 return r.status_code
print getStatusCode('//www.jb51.net/')

相关文章

python os.path.isfile 的使用误区详解

下列这几条语句,看出什么问题了不? for file in os.listdir(path): if os.path.isfile(file) and os.path.spl...

python 数据生成excel导出(xlwt,wlsxwrite)代码实例

这篇文章主要介绍了python 数据生成excel导出(xlwt,wlsxwrite)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以...

高质量Python代码编写的5个优化技巧

如今我使用 Python 已经很长时间了,但当我回顾之前写的一些代码时,有时候会感到很沮丧。例如,最早使用 Python 时,我写了一个名为 Sudoku 的游戏(GitHub地址:ht...

Python中的map、reduce和filter浅析

1、先看看什么是 iterable 对象 以内置的max函数为例子,查看其doc:复制代码 代码如下:>>> print max.__doc__max(iterable...

利用arcgis的python读取要素的X,Y方法

如下所示: import arcpy ... from arcpy import env ... env.workspace="C:\\Users\\Administrator\\D...