import requests
#step1 指定url
url='https://love.163.com/'
#step2 发送GET请求
response=requests.get(url=url)
#step3 获取响应数据,text:字符串格式
page_code=response.text
with open('youtian.html','w',encoding='utf-8') as fp:
fp.write(page_code)
print('爬取源码完成')
code摘要: with 后面接表达式,写入文件先open,带上参数‘w’和编码utf-8,不然提示无文件与乱码
fp是表达式的对象,page_code获取返回内容