Python基础

Ubuntu环境

安装依赖

1
2
$ sudo apt-get install build-essential checkinstall
$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

安装Python

1
2
3
4
5
6
7
8
$ cd /usr/local
$ sudo wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz
$ sudo tar zxvf Python-3.6.7.tgz
$ cd Python-3.6.7/
$ sudo ./configure
$ sudo make
$ sudo make install
$ python3 -V

安装pip3

1
2
3
$ sudo apt-get install curl
$ curl https://bootstrap.pypa.io/get-pip.py | sudo python3
$ pip3 -V

CentOS环境

安装依赖

1
$ yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl--devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

安装Python3

1
2
3
4
5
6
7
8
9
$ cd /usr/local/
$ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
$ tar -zxvf Python-3.6.1.tgz
$ cd Python-3.6.1/
$ mkdir /usr/Python-3.6.1
$ ./configure --prefix=/usr/Python-3.6.1 --with-openssl=/usr/local/openssl
$ make && make install
$ ln -s /usr/Python-3.6.1/bin/python3 /usr/bin/python3
$ python3 -V

安装pip3

方法1

python3安装完成后默认已经带有pip3。 你可以用以下命令,创建软链接

1
$ ln -s /usr/Python-3.6.1/bin/pip3 /usr/bin/pip3

方法2

1
2
3
4
$ wget https://bootstrap.pypa.io/3.2/get-pip.py
$ python3 get-pip.py
$ ln -s /usr/python3.6.1/bin/pip /usr/bin/pip3
$ pip3 -V

更新pip3

1
$ pip3 install --upgrade pip

Mac环境

安装Python3

1
2
3
4
5
6
$ sudo mkdir /usr/local/Frameworks
$ sudo chown $(whoami):admin /usr/local/Frameworks
$ brew install python3
# 测试验证
$ python3
$ pip3 -V

安装请求库

安装requests

1
2
3
4
$ pip3 install requests
# 测试验证
$ python3
>>> import requests

安装Selenium

1
2
3
4
$ pip3 install selenium
# 测试验证
$ python3
>>> import selenium

安装aiohttp

1
2
3
4
5
6
$ pip3 install aiohttp
# 官方推荐安装如下两个库,cchardet是字符编码检测库,aiodns是加速DNS解析的库。
$ pip3 install cchardet aiodns
# 测试验证
$ python3
>>> import aiohttp

安装解析库

安装lxml

1
$ pip3 install lxml

安装Beautiful Soup

1
$ pip3 install beautifulsoup4

安装pyquery

1
$ pip3 install pyquery

安装tesserocr

1
2
3
$ brew install imagemagick
$ brew install tesseract --with-all-languages
$ pip3 install tesserocr pillow

安装存储库

安装PyMySQL

1
2
3
4
5
6
$ pip3 install pymysql
# 测试验证
$ python3
>>> import pymysql
>>> pymysql.VERSION
(0, 9, 2, None)

安装PyMongo

1
2
3
4
5
6
$ pip3 install pymongo
# 测试验证
$ python3
>>> import pymongo
>>> pymongo.version
'3.7.1'

安装redis-py

1
2
3
4
5
6
$ pip3 install redis
# 测试验证
$ python3
>>> import redis
>>> redis.VERSION
(2, 10, 6)

安装Web库

安装flask

1
$ pip3 install flask

安装tornado

1
$ pip3 install tornado

安装爬虫框架

安装Scrapy

1
2
$ xcode-select --install
$ pip3 install Scrapy

安装部署库

安装Scrapyd

1
$ pip3 install scrapyd

常见错误

InsecureRequestWarning

问题描述

1
2
/usr/local/python/lib/python3.5/site-packages/urllib3/connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)

解决办法

  • requests前加入:requests.packages.urllib3.disable_warnings()
  • requests添加verify=False参数
1
2
3
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
requests.get('https://xxx.com',verify=False)

SyntaxError: Non-ASCII character '5' in file

问题描述

1
SyntaxError: Non-ASCII character '\xe5' in file

问题原因

Python默认是以ASCII作为编码方式的,如果在自己的Python源码中包含了中文,就需要指定编码格式。 ### 解决办法 将下面代码加载文件第一行。

1
# -*- coding: UTF-8 -*-

参考

Request模块

文章

坚持原创技术分享,您的支持将鼓励我继续创作!
0%