基于python3,获取网络数据,并对数据进行字符编码
# 先安装request框架:pip install requests
import requests
url = "http://www.test.html"
cookies = {"_gitlab_session":"a167f84b5e34c08b8d33db5a4ee94d92"}
res = requests.get(url, cookies=cookies) #带入cookie参数,以获得登录态
content = res.content; #数据类型为:byte
content = content.decode() #转换为str
content = content.strip().replace("\n", "") #去除空格和换行
f = open('content.txt', 'w', encoding='utf-8') #encoding要指定为'utf-8',不然会编码错误
f.write(content)
f.close()