Python学习笔记--爬虫

1.编码方式

Unicode为了执行统一标准,将所有国家的编码方式加了进来,例如常用的UTF-8就是Unicode的一种实现方式,他是可变长编码

简单地说,就是当文本是ASCII编码的自复式,他只用一字节存放,而当文本是其他Unicode字符是,他按照一定算法转换,每个字符使用1-3字节存放,这样便有了有效节省空间的目的。

2.Requests

在Python中还有更好的http库--Requests

(1)安装问题

最开始安装Requests时,输入pip install requests报错,提示unknown or unsupported command。然后百度知道是电脑中有多个pip.exe,所以计算机不知道调用哪个。

执行以下命令,问题解决。

 
安装成功后,测试一波。
(2)利用requests,可以轻松得到htttp响应码和响应头等信息。
#Requests
import requests
r=requests.get('http://baidu.com',auth=('user','pass'))
print("------------------------------")
print(r.status_code)
print(r.headers)

运行结果:

200
{'Date': 'Tue, 03 Nov 2020 08:40:24 GMT', 'Server': 'Apache', 'Last-Modified': 'Tue, 12 Jan 2010 13:48:00 GMT', 'ETag': '"51-47cf7e6ee8400"', 'Accept-Ranges': 'bytes', 'Content-Length': '81', 'Cache-Control': 'max-age=86400', 'Expires': 'Wed, 04 Nov 2020 08:40:24 GMT', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}

 

posted @ 2020-11-03 17:06  菜鸡要加油  阅读(62)  评论(0编辑  收藏  举报