import ssl
import requests
url="https://www.bilibili.com"
headers={
"User-Agent":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45."
}
def use_requests(url):
response=requests.get(url,headers=headers)
data=response.text
# print(data)
file_path="D:/Pycharm/file/bilibili/bilibili.html"
with open(file_path,"w",encoding="utf-8") as f:
f.write(data)
if __name__=='__main__':
use_requests(url)
打开文件:with open(file_path,"w",encoding="utf-8") as f中的w参数说明:
本地windows系统默认编码GBK,python默认编码utf8,设定编码方式不会出现乱码。
网友评论