首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >python取余什么意思_python中取余

python取余什么意思_python中取余

作者头像
全栈程序员站长
发布2022-08-10 10:48:11
发布2022-08-10 10:48:11
1.8K0
举报

大家好,又见面了,我是你们的朋友全栈君。

首先取响应头里的编码,如果是几种中文编码之一,则认为中文网页,如果不是中文编码, 也不是几种unicode方案之一, 则不是中文.否则再在标签里找 charset ,如果有并且为几种中文编码之一, 则是中文如果不是中文编码, 也不是几种unicode方案之一, 则不是中文.否则对body的内容(如果考虑性能问题,可以不对整个body,只对前N个字节)用正则洗标签过滤所有ASCII码字符, 剩余部分按字取内码,如果考虑性能问题其实取第一个字符就可以了,如果性能不重要,可以多采样几个(防止一个页面有中文日文等各种文字混合)判断采到的字符的内码是否位于中文unicode区域.如果性能非常不重要, 只是要代码简单, 那么做法可以: 正则 1 这样可以取到第一个不是ASCII的字符, 判断这个字符是否是中文字符就可以了参考代码:#! /usr/bin/env python

# -*- encoding: utf-8 -*-

import requests

import cld2

class Detector(object):

zh_cn_encodes = (‘gbk’, ‘gb2312’, ‘gb18030’)

def __init__(self, context):

self.context = context

def is_zh_cn_encoding(self, ctype):

ctype = ctype.lower()

for ec in self.zh_cn_encodes:

if ec in ctype:

return True

return False

def detect(self):

assert type(self.context) == type(u”), ‘detect unicode string only’

ret = cld2.detect(self.context.encode(‘utf-8’))

return ret[2][0][1] == ‘zh’

def url_detect(self):

r = requests.get(self.context)

assert r.status_code == 200, ‘http code 200 is required’

ctype = r.encoding

if ctype and self.is_zh_cn_encoding(ctype):

return True

else:

self.context = r.text

return self.detect()

if __name__ == ‘__main__’:

print Detector(u’短中文有bug’).detect()

print Detector(u’网页文件一般没问题’).detect()

print Detector(u’これは日本で’).detect()

url = ‘http://segmentfault.com/q/1010000000432652’

print Detector(url).url_detect()

url = ‘https://code.google.com/p/chromium-compact-language-detector/source/browse/README’

print Detector(url).url_detect()

x00-xff ↩

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129873.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档