zoukankan      html  css  js  c++  java
  • 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)版就介绍到这里了

    更多文章链接:Python 爬虫随笔


    - 本笔记不允许任何个人和组织转载
  • 相关阅读:
    寒假作业1实践题
    寒假作业1问答题
    (1.21)思考题
    [golang note] 匿名组合
    [golang note] 类型系统
    [golang note] 错误处理
    [golang note] 函数定义
    [golang note] 流程控制
    [golang note] 内建类型
    [golang note] 数组切片
  • 原文地址:https://www.cnblogs.com/xpwi/p/9600645.html
Copyright © 2011-2022 走看看