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 requestsimport jsondef 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)
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

代码运行这里写图片描述

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

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


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

           

猜你喜欢

转载自blog.csdn.net/qq_44906554/article/details/89328454