初学爬虫

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值