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 利用已有Ner模型进行数据清洗合并代码

我就废话不多说了,直接上代码吧! # -*- coding: utf-8 -*- from kashgari.corpus import DataReader import re f...

Django ManyToManyField 跨越中间表查询的方法

1、在 django 表中用到了 manytomany 生成了中间表 pyclub_article_column from django.db import models # Cr...

python中二维阵列的变换实例

本文实例讲述了python中二维阵列的变换方法。分享给大家供大家参考。具体方法如下: 先看如下代码: arr = [ [1, 2, 3], [4, 5, 6], [7, 8,9],...

Python字符遍历的艺术

比如,将一个字符串转换为一个字符数组: theList = list(theString) 同时,我们可以方便的通过for语句进行遍历: for c in theString: do_s...

Django中使用haystack+whoosh实现搜索功能

Django中使用haystack+whoosh实现搜索功能

为了实现项目中的搜索功能,我们使用的是全文检索框架haystack+搜索引擎whoosh+中文分词包jieba 安装和配置 安装所需包 pip install django-hays...