• 爬取百度翻译结果


    思路

    用到的模块

    requests模块
    

    requests模块:python中原生的一款基于网络请求的模块,功能非常强大,简单便捷,效率极高。
    作用:模拟浏览器发请求。
    如何使用:requests模块的编码流程

        - 指定url
            - UA伪装
            - 请求参数的处理
        - 发起请求
        - 获取响应数据
        - 持久化存储
    

    实现代码

    #Author:Hel10
    #time:
    #content:爬取百度翻译的结果
    import requests
    import json
    if __name__ == '__main__':
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0'
        }  #进行UA伪装
        url = "https://fanyi.baidu.com/sug"    #指定url
        word = input("输入你要翻译的单词:")      #post请求的参数
        data = {
            "kw":word
        }
        response = requests.post(url=url,data=data,headers=headers) #进行post请求
        dict_obj = response.json()  #获取json数据,这个可以在请求头的content-type字段可以看到
        # 持久化储存
        filename = word+".json"
        fp = open(filename,'w',encoding='utf-8')
        json.dump(dict_obj,fp=fp,ensure_ascii=False)
        print("---------------翻译结束,请在根目录下查看结果---------------------")
    

    翻译结果存储为json数据。

    遇到的问题

    (Caused by SSLError("Can't  connect to HTTPS URL because the SSL module is not available."))
    

    解决方法:在stackoverflow上面看到好多的方法,全怼了一遍,也不知道到底是那种方法解决了这种报错。
    https://stackoverflow.com/questions/54135206/requests-caused-by-sslerrorcant-connect-to-https-url-because-the-ssl-module
    1、在环境变量和用户变量里面都加了anaconda的路径

    <path>Anaconda3
    <path>Anaconda3scripts
    <path>Anaconda3Libraryin
    

    2、把D:AnacondaLibraryin目录下的

    libcrypto-1_1-x64.*
    libssl-1_1-x64.*
    

    复制到了D:Anaconda3DLLs。
    3、升级了一下anaconda的版本。参考这个 https://zhuanlan.zhihu.com/p/121601968

  • 相关阅读:
    Vue状态管理
    Vue延迟点击
    Vue路由
    简单的队列应用
    Uncaught SyntaxError: Unexpected token )
    视频转码
    判断是否为视频文件
    Press ^C at any time to quit.
    Node.js学习
    YUM安装LAMP与LNMP
  • 原文地址:https://www.cnblogs.com/HelloCTF/p/14029574.html
Copyright © 2020-2023  润新知