Python爬虫教程-06-爬虫实现百度翻译(requests)

使用python爬虫实现百度翻译(requests)

python爬虫
  • 上一篇介绍了怎么使用浏览器的【开发者工具】获取请求的【地址、状态、参数】以及使用python爬虫实现百度翻译功能【urllib】版
  • 上一篇链接:https://blog.csdn.net/qq_40147863/article/details/81590849
  • 本篇介绍使用python爬虫实现百度翻译功能【requests】版

使用requests,必须先添加requests包

  • 安装requests
  • 如果使用Anaconda环境,使用下面命令:
    conda install requests
    如果不是,就自己手动在【PyCharm】>【file】>【settings】>【Project Interpreter】>【+】>【requests】>【install】
    具体操作截图:
    这里写图片描述
    这里写图片描述

废话少说,直接献上代码

# 百度翻译
# 添加包的方法在上面
import requests
import json

def fanyi(keyword):
    url = 'http://fanyi.baidu.com/sug'

    # 定义请求参数
    data = {
        'kw': keyword
    }

    # 发送请求,抓取信息
    res = requests.post(url,data=data)

    # 解析结果并输出
    str_json = res.text

    myjson = json.loads(str_json)
    info = myjson['data'][0]['v']
    print(info)

if __name__=='__main__':
    while True:
        keyword = input('请输入翻译的单词:')
        if keyword == 'q':
            break
        fanyi(keyword)

代码运行这里写图片描述

没错,requests版就是这么简洁,只不过需要加载requests包
使用python爬虫实现百度翻译功能(requests)版就介绍到这里了
拜拜


- 本笔记不允许任何个人和组织转载

猜你喜欢

转载自blog.csdn.net/qq_40147863/article/details/81591145