1.十转二
将十进制转换为二进制:
>>> bin(10)
'0b1010'
2.十转八
十进制转换为八进制:
>>> oct(9)
'0o11'
3 十转十六
十进制转换为十六进制:
>>> hex(15)
'0xf'
4.字符串转字节
字符串转换为字节类型
>>> s = "apple"
>>> bytes(s,encoding='utf-8')
b'apple'
课程1.py
源代码如下:
爬三国演义:
with open('三国演义.txt','a+') as f:
f.write(novel)
f.close()
爬图片:
import requests
response = requests.get('https://res.pandateacher.com/2019-01-12-15-29-33.png ')
photo = response.content
k = open('图片.jpg','wb')
k.write(photo)
k.close()
爬VIP音乐
import requests
# 发出请求,并把返回的结果放在变量res中
res = requests.get('https://static.pandateacher.com/Over%20The%20Rainbow.mp3')
# 把Reponse对象的内容以二进制数据的形式返回
mp3 = res.content
# 新建了一个文件rainbow.mp3,这里的文件没加路径,它会被保存在程序运行的当前目录下。
# 音频内容需要以二进制wb读写。你在学习open()函数时接触过它。
music = open('rainbow.mp3','wb')
# 写入mp3的二进制内容
music.write(mp3)
# 关闭文件
music.close()
爬超文本链接:
import requests
response = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
print(response.status_code)
k = response.text
file = open('book.html','w')
file.write(k)
file.close()
python基础,爬虫工程师,web开发,dango实战资源