程序员之所以犯错误,不是因为他们不懂,而是因为他们自以为什么都懂。

print(isinstance([1, 2, 3], Iterable))
print(isinstance({}, Iterable))
print(isinstance(123, Iterable))
print(isinstance('abc', Iterable))print(isinstance([1, 2, 3], Iterator))

结果 :

True
True
False
True
False

使用isinstance判断一个对象的类型,这里判断对象是不是可迭代的,发现数字和数组是不可迭代的

g = (x * x for x in range(10))
print(type(g))
print(isinstance(g, Iterable))
print(isinstance(g, Iterator))
for i in g:print(i)

结果 :

<class 'generator'>
True
True
0
1
4
9
16
25
36
49
64
81

上一节generator函数,他是可迭代的,属于迭代器

def fib(limit):n, a, b = 0, 0, 1while n < limit:yield ba, b = b, a + bn += 1return 'done'f = fib(6)
print(type(f))
print(isinstance(f, Iterable))
print(isinstance(f, Iterator))
for i in f:print(i)

结果 :

<class 'generator'>
True
True
1
1
2
3
5
8

使用tield生成generator

print((1024).to_bytes(2, byteorder = 'big'))
print((65536).to_bytes(8, byteorder = 'little'))
print((-1024).to_bytes(4, byteorder = 'big', signed = True))
print((-1024).to_bytes(4, byteorder = 'little', signed = True))
print((500).to_bytes(2, byteorder = 'big'))
print((3345).to_bytes(2, byteorder = 'big'))    # why \r\x11
print((3124).to_bytes(2, byteorder = 'big'))    # why \x0c4 => \x0c + 4(0x34)
print((3140).to_bytes(2, byteorder ='little'))  # why D\x0c => D(0x44) + 0x0c
print('%x' % 3345)
print('%x' % 3124)
print(0xd11)
print(0xc34)

结果 :

b'\x04\x00'
b'\x00\x00\x01\x00\x00\x00\x00\x00'
b'\xff\xff\xfc\x00'
b'\x00\xfc\xff\xff'
b'\x01\xf4'
b'\r\x11'
b'\x0c4'
b'D\x0c'
d11
c34
3345
3124

整数和byte数组的转换,使用to_bytes,注意位数不要太小,如果承载不下会报错

b = b'china\r\nus'
print(type(b))
s = b.decode()
print(s)
print(s.encode())

结果 :

<class 'bytes'>
china
us
b'china\r\nus'

字符串和byte数组的转换,使用.decode()可以把bytes转换成字符串,使用encode()转换成bytes

import requestsfrom xml.parsers.expat import ParserCreateclass DefaultSaxHandler(object):def __init__(self, provinces):self.provinces = provincesdef start_element(self, name, attrs):print("name=",name,"  attrs=",attrs)if name != 'map':name = attrs['title']number = attrs['href']self.provinces.append((name, number))def end_element(self, name ):print("end_element",name)def char_data(self, text):print("char_data",text)def get_provinces(url):content = requests.get(url).content.decode('gb2312')start = content.find('<map name=\"map_86\" id=\"map_86\">')print("开始",start)end = content.find('</map>')print("结束",end)content = content[start:end + len('</map>')].strip()print("内容",content)provinces = []href=[]handler = DefaultSaxHandler(provinces)parser = ParserCreate()parser.StartElementHandler = handler.start_elementparser.EndElementHandler = handler.end_elementparser.CharacterDataHandler = handler.char_dataparser.Parse(content)return provincesprovinces = get_provinces('http://www.ip138.com/post')
print(provinces)

结果 :

[('新疆', '/83/'), ('西藏', '/85/'), ('青海', '/81/'), ('甘肃', '/73/'), ('四川', '/61/'), ('云南', '/65/'), ('宁夏', '/75/'), ('内蒙古', '/01/'), ('黑龙江', '/15/'), ('吉林', '/13/'), ('辽宁', '/11/'), ('河北', '/50/'), ('北京', '/10/'), ('天津', '/30/'), ('陕西', '/71/'), ('山西', '/03/'), ('山东', '/25/'), ('河南', '/45/'), ('重庆', '/40/'), ('湖北', '/43/'), ('安徽', '/23/'), ('江苏', '/21/'), ('上海', '/20/'), ('贵州', '/55/'), ('广西', '/53/'), ('湖南', '/41/'), ('江西', '/33/'), ('浙江', '/31/'), ('福建', '/35/'), ('广东', '/51/'), ('海南', '/57/'), ('台湾', '/taiwang/'), ('澳门', '/aomen/'), ('香港', '/xianggang/')]

上面程序是做了一个简单的爬虫,我们先打开网页看看

这是一个查邮政编码的网站,点击下方地图会进入到对应地区的网址,比如新疆

http://www.ip138.com/83/

编号是83,我们要做的就是抓取各个地区的编号

我们使用requests来下载网页,解码为gb2312,找到页面地图所在位置,使用.find可以找到匹配字符串的位置,然后吧第一行map标签掠过,解析后面area标签,得到一个字典,从字典中依次取出title和href,组成元组放到数组中

Python入门程序【四】相关推荐

  1. python 入门程序_非Python程序员的Python速成课程-如何快速入门

    python 入门程序 This article is for people who already have experience in programming and want to learn ...

  2. Python入门程序

    二.Python入门程序 2.1 Hello Python程序 2.1.1 Python 源程序的基本概念 1.Python源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python的 ...

  3. Python入门程序 字符串应用(学号判断程序、密码破解程序、身份证的秘密)

    Python入门程序 字符串应用(学号判断程序.密码破解程序.身份证的秘密) 没想到学校会在大二同时学习Java的的时候一起学Python语言,在这写一些平时的python编程作业. 就从字符串的应用 ...

  4. Python入门程序练习题-温度转换

    Python入门程序练习题-温度转换 题目说明: 温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fabrenheit).‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪ ...

  5. Python入门(四)- 面向对象及关键字

    读者肯定听过 Python 中"一切皆对象"的说法,但可能并不了解它的具体含义,只是在学习的时候听说 Python 是面向对象的编程语言,本节将向大家详细介绍 Python 面向对 ...

  6. python入门程序有趣例子_10 个最值得 Python 新人练手的有趣项目

    原标题:10 个最值得 Python 新人练手的有趣项目 作者 | Claire D. Costa 编译 | Wendy 有很多 Python 新手留言问:"Python 入门很久了,但项目 ...

  7. python入门(四)小康小白

    我是小康小白,一个平平无奇的Java小白.热爱有趣的文字,生活和远方. 个人博客:https://blog.csdn.net/weixin_45791445 有问题欢迎QQ联系:1059320343 ...

  8. python入门程序异常_Python入门基础(10)_异常_1

    最近有点忙,到现在快一个月没写了,罪过罪过,继续学习 异常:python程序在运行时,如果python解释器遇到一个错误,那么程序就会停止执行,并且会提示一些错误信息,这就是异常. 抛出异常:程序停止 ...

  9. python入门程序异常_Python 入门 之 异常处理

    1.异常处理 (1)程序中的错误分为两种 <1> 语法错误 (这种错误,根本过不了Python解释器的语法检测,必须在程序执行前就改正) # 语法错误示范 print(111 [1;2;3 ...

最新文章

  1. 如何编写webService接口
  2. 在学习中遇到的第一难点
  3. Java编程思想 第十三章:字符串
  4. 【Android】 Android中ListView使用详解
  5. mysql安装教程刘猿猿_mysql安装
  6. Python 局部变量和全局变量 - Python零基础入门教程
  7. 在java中对于构造函数_在Java语言中,下面有关于构造函数的描述正确的是()。
  8. 爆料人透露苹果正开发可折叠iPhone:两块独立屏幕连接 无刘海
  9. [Python] np.array() 创建ndarray类型的数组
  10. java ajax cookies_HttpOnly cookie如何处理AJAX请求?
  11. [Java设计模式]单例模式
  12. 易飞ERP工作流解决方案之【第三方OA系统集成】
  13. 表格里加横线一分为二_表格分割线如何一分为二
  14. 博客备份工具:Blog_Backup
  15. Largest Rectangle in a Histogram[]
  16. java相关资料下载
  17. xshell 免费版申请
  18. Redis高可用架构
  19. RGB颜色空间转LAB
  20. 常见的几种身份验证方法

热门文章

  1. 华为AITO M7新车来袭,会搭载鸿蒙吗?
  2. MATLAB中果蝇味道浓度判定函数,果蝇优化算法MATLAB实现
  3. 电脑修复网络的命令是什么
  4. 什么样的音乐平台能够激发用户付费,Spotify告诉你答案
  5. 用 Delphi 做个发信机
  6. Safari浏览器display: flex布局错乱,Chrome布局正确
  7. Python 方格子Ising模型模拟
  8. Python 爬虫-新浪微博
  9. 阅读类APP可行的广告变现入口分析
  10. komodo 中文_使用Komodo提高生产力:可扩展的多语言IDE