zoukankan      html  css  js  c++  java
  • python常见错误

    1.PyCharm项目no python interpreter configured

    错误:Cannot run program "D:Python363python.exe" (in directory "E:PythonWorkSpace estInterface"): CreateProcess error=2, 系统找不到指定的文件。

    解决方案

    Run>>>Edit Configurations
    检查对应信息是否正确
    在检查以上信息后,有可能不需要修改任何东西,直接点击Apply >>>Ok ,重新运行就可以正常运行不会有错误提示

    2.pycharm修改默认python版本

    今天安装Django的时候遇到了python版本冲突,找不到python路径,所以又重新安装了一个python3.6.5

     安装完之后,突然发现自己的pycharm是之前Anaconda的3.5版本,那么就需要修改一下python版本了

     首先点击左上角的File,再点击Default Settings,点击右侧Project Interpreter框框的下箭头,然后点击Show  All

     然后点击右侧绿色的+号,就能够添加新的地址,记住勾选就行。

    3.Could not find a version that satisfies the requirement requests (from version

    s: )
    No matching distribution found for requests

    可能考虑到是python国内网络的问题,这时我们用国内的镜像源来加速。

    pip install requests  -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

    4、requests爬虫测试接口返回html并且中文显示乱码

    python抓取网页并写到本地文件中,出现乱码问题的关键有两处:

    1. 抓取网页数据后是否正确解码
    2. 正确解码后的html字符串写入文件时是否正确编码
      要解决这两个问题,首先要知道该网页是如何编码的,先看看下面代码:
    import requests
    head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) Firefox/21.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,zh-CN,zh;q=0.9"
    }
    url = 'http://2018.ip138.com/ic.asp'
    resp=requests.get(url, headers=head,timeout=15)
    print(resp.headers['content-type'])
    print(resp.encoding)
    print(resp.apparent_encoding)
    print(requests.utils.get_encodings_from_content(resp.text))
    

    输出结果:
    在这里插入图片描述
    这里的apparent_encoding是响应消息头里设置的编码格式,最后一个是html源码里设置编码格式,这两个我观察是一样的(不确定有没有不一样的,如果不一样,我觉得网页直接打开可能就是乱码),这里的apparent_encoding就是网页数据的编码格式,我们只要使用apparent_encoding来解码和编码,一般就不会出现乱码了。
    使用方法如下:

    resp = requests.get(url, headers=head,timeout=15)
    resp.encoding=resp.apparent_encoding   #设置解码方式
    html_source=resp.text      #这里会用设置的解码方式解码
    print(html_source)	      #正确解码后直接打印也不会出现乱码
    resp.close()
    if html_source:
    #将html写文件时设置文件编码方式,因为和html里设置的编码格式一样,所以直接打开就不会出现乱码了
        with open('test.htm', mode='a+', encoding=resp.apparent_encoding) as file:
    file.write(html_source)
    转自:https://blog.csdn.net/qq_33440662/article/details/82787301?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160400350319725255515369%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=160400350319725255515369&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-2-82787301.pc_first_rank_v2_rank_v28&utm_term=python3+%E7%88%AC%E7%BD%91%E9%A1%B5%E8%BF%94%E5%9B%9E%E4%B9%B1%E7%A0%81&spm=1018.2118.3001.4449

     5、SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape

    报错信息:File "E:/PythonWorkSpace/TestInterface2/test.py", line 29

    cookie_file = 'C:Users{UserName}AppDataLocalGoogle'
    ^
    SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape

    解决方案:

    目前有3个解决方案

    1、在路径前面加r,即保持字符原始值的意思。

    sys.path.append(r 'C:Users{UserName}AppDataLocalGoogle')

    2、替换为双反斜杠

    sys.path.append('c:\Users\mshacxiang\VScode_project\web_ddt')

    3、替换为正斜杠

    sys.path.append('c:/Users/mshacxiang/VScode_project/web_ddt')
  • 相关阅读:
    JavaScript——实现compose函数
    Typora——如何画流程图 | mermaid-js
    Electron——复制文件操作
    JavaScript——实现一些常用函数
    vue elementUI表单主动trigger某个rules校验
    [java]多线程——多线程debug调试(非常非常详细的调试)
    CompletableFuture supplyAsync() and thenApply() 用法区别
    CAS和MySql乐观锁实现下单
    TiDB集群手动安装
    Vue中 let _this = this的作用
  • 原文地址:https://www.cnblogs.com/ITGirl00/p/13900039.html
Copyright © 2011-2022 走看看