import json
import requests
response=requests.get("https://tieba.baidu.com/fkw=%E6%B5%81%E6%B5%AA%E6%B1%89")
#w+ 检测到文件不存在就自己创建
with open('tieba.html','w+',encoding='utf-8')as f:
f.write(response.text) ---->string
#f.write(response.content.decode('utf-8'))
#解释上面with 方法
class WithObject(object):
当对象里面实现了enter和exit方法 可以用with 不用的时候可以自己关闭
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
pass
GET:一般用来获取数据 它的参数一般放在url后面
POST:一般用来向服务器提交数据,它的参数存放在消息体里,更安全一些.
# POST
req_url = "http://fanyi.youdao.com/translate" # 创建连接接口
Form_Date = {}
Form_Date['i'] = 'python is best' # 要翻译的内容可以更改
Form_Date['doctype'] = 'json'
response = requests.post(req_url,data=Form_Date)
html = response.text
html = json.loads(html)
print(html)