升级macOS Python2至Python3

查看当前版本

python -v

显示当前版本是python 2.7

Python 2.7.10 (default, Oct  6 2017, 22:29:07)

[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

dlopen(“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so”, 2);

import readline # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so

urllib很多库的语法做了变更,需要升级为Python3 , upgrade就是了

  1. 安装brew

    /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

  2. brew install python3

    hongdaqideAir:~ hongdaqi$ brew install python3

    ==> Installing dependencies for python: gdbm, openssl, readline, sqlite and xz

    ==> Installing python dependency: gdbm

    ==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1.high_sierra.bottle.1.tar.gz

    ######################################################################## 100.0%

    ==> Pouring gdbm-1.18.1.high_sierra.bottle.1.tar.gz

    🍺  /usr/local/Cellar/gdbm/1.18.1: 20 files, 590.8KB

    ==> Installing python dependency: openssl

    ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2s.high_sierra.bottle.tar.gz

    ==> Downloading from https://akamai.bintray.com/b7/b72b8d9e582713d909936d7236542b366f07d800f8ec0eaa2d487a95c4e93bd9?__gda__=exp=1566488184~hmac=b30f

    ######################################################################## 100.0%

    ==> Pouring openssl-1.0.2s.high_sierra.bottle.tar.gz

    ==> Caveats

    A CA file has been bootstrapped using certificates from the SystemRoots

    keychain. To add additional certificates (e.g. the certificates added in

    the System keychain), place .pem files in

      /usr/local/etc/openssl/certs

    and run

      /usr/local/opt/openssl/bin/c_rehash

    openssl is keg-only, which means it was not symlinked into /usr/local,

    because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

    If you need to have openssl first in your PATH run:

      echo ‘export PATH=”/usr/local/opt/openssl/bin:$PATH”‘ >> ~/.bash_profile

    For compilers to find openssl you may need to set:

      export LDFLAGS=”-L/usr/local/opt/openssl/lib”

      export CPPFLAGS=”-I/usr/local/opt/openssl/include”

    ==> Summary

    🍺  /usr/local/Cellar/openssl/1.0.2s: 1,795 files, 12.1MB

    ==> Installing python dependency: readline

    ==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.0_1.high_sierra.bottle.tar.gz

    ==> Downloading from https://akamai.bintray.com/7a/7a45c1ed8488b6832f067adffaab328d643090d118f722f59ce4651731e10f1c?__gda__=exp=1566488263~hmac=06dd

    ######################################################################## 100.0%

    ==> Pouring readline-8.0.0_1.high_sierra.bottle.tar.gz

    ==> Caveats

    readline is keg-only, which means it was not symlinked into /usr/local,

    because macOS provides the BSD libedit library, which shadows libreadline.

    In order to prevent conflicts when programs look for libreadline we are

    defaulting this GNU Readline installation to keg-only.

    For compilers to find readline you may need to set:

      export LDFLAGS=”-L/usr/local/opt/readline/lib”

      export CPPFLAGS=”-I/usr/local/opt/readline/include”

    ==> Summary

    🍺  /usr/local/Cellar/readline/8.0.0_1: 48 files, 1.5MB

    ==> Installing python dependency: sqlite

    ==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.29.0.high_sierra.bottle.tar.gz

    ==> Downloading from https://akamai.bintray.com/dc/dcdc548263b6a8611d0e3532da5e216399cbd51e04277bb1ec9130fbb1125994?__gda__=exp=1566488303~hmac=1c0e

    ######################################################################## 100.0%

    ==> Pouring sqlite-3.29.0.high_sierra.bottle.tar.gz

    ==> Caveats

    sqlite is keg-only, which means it was not symlinked into /usr/local,

    because macOS provides an older sqlite3.

    If you need to have sqlite first in your PATH run:

      echo ‘export PATH=”/usr/local/opt/sqlite/bin:$PATH”‘ >> ~/.bash_profile

    For compilers to find sqlite you may need to set:

      export LDFLAGS=”-L/usr/local/opt/sqlite/lib”

      export CPPFLAGS=”-I/usr/local/opt/sqlite/include”

    ==> Summary

    🍺  /usr/local/Cellar/sqlite/3.29.0: 11 files, 3.9MB

    ==> Installing python dependency: xz

    ==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.4.high_sierra.bottle.tar.gz

    ==> Downloading from https://akamai.bintray.com/e7/e7be50f4ee00e35887f3957263334eb3baba59e8c061919060f9259351be6880?__gda__=exp=1566488309~hmac=9a6d

    ######################################################################## 100.0%

    ==> Pouring xz-5.2.4.high_sierra.bottle.tar.gz

    🍺  /usr/local/Cellar/xz/5.2.4: 92 files, 1MB

    ==> Installing python

    ==> Downloading https://homebrew.bintray.com/bottles/python-3.7.4.high_sierra.bottle.tar.gz

    ==> Downloading from https://akamai.bintray.com/65/6525b29fcaac48e3467efef273dca0d883a164a1e50cc36870cb407bc71f4133?__gda__=exp=1566488315~hmac=0c08

    ######################################################################## 100.0%

    ==> Pouring python-3.7.4.high_sierra.bottle.tar.gz

    ==> /usr/local/Cellar/python/3.7.4/bin/python3 -s setup.py –no-user-cfg install –force –verbose –install-scripts=/usr/local/Cellar/python/3.7.4/

    ==> /usr/local/Cellar/python/3.7.4/bin/python3 -s setup.py –no-user-cfg install –force –verbose –install-scripts=/usr/local/Cellar/python/3.7.4/

    ==> /usr/local/Cellar/python/3.7.4/bin/python3 -s setup.py –no-user-cfg install –force –verbose –install-scripts=/usr/local/Cellar/python/3.7.4/

    ==> Caveats

    Python has been installed as

      /usr/local/bin/python3

    Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to

    `python3`, `python3-config`, `pip3` etc., respectively, have been installed into

      /usr/local/opt/python/libexec/bin

    If you need Homebrew’s Python 2.7 run

      brew install python@2

    You can install Python packages with

      pip3 install <package>

    They will install into the site-package directory

      /usr/local/lib/python3.7/site-packages

    See: https://docs.brew.sh/Homebrew-and-Python

    ==> Summary

    🍺  /usr/local/Cellar/python/3.7.4: 3,865 files, 60MB

    ==> Caveats

    ==> openssl

    A CA file has been bootstrapped using certificates from the SystemRoots

    keychain. To add additional certificates (e.g. the certificates added in

    the System keychain), place .pem files in

      /usr/local/etc/openssl/certs

    and run

      /usr/local/opt/openssl/bin/c_rehash

    openssl is keg-only, which means it was not symlinked into /usr/local,

    because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

    If you need to have openssl first in your PATH run:

      echo ‘export PATH=”/usr/local/opt/openssl/bin:$PATH”‘ >> ~/.bash_profile

    For compilers to find openssl you may need to set:

      export LDFLAGS=”-L/usr/local/opt/openssl/lib”

      export CPPFLAGS=”-I/usr/local/opt/openssl/include”

    ==> readline

    readline is keg-only, which means it was not symlinked into /usr/local,

    because macOS provides the BSD libedit library, which shadows libreadline.

    In order to prevent conflicts when programs look for libreadline we are

    defaulting this GNU Readline installation to keg-only.

    For compilers to find readline you may need to set:

      export LDFLAGS=”-L/usr/local/opt/readline/lib”

      export CPPFLAGS=”-I/usr/local/opt/readline/include”

    ==> sqlite

    sqlite is keg-only, which means it was not symlinked into /usr/local,

    because macOS provides an older sqlite3.

    If you need to have sqlite first in your PATH run:

      echo ‘export PATH=”/usr/local/opt/sqlite/bin:$PATH”‘ >> ~/.bash_profile

    For compilers to find sqlite you may need to set:

      export LDFLAGS=”-L/usr/local/opt/sqlite/lib”

      export CPPFLAGS=”-I/usr/local/opt/sqlite/include”

    ==> python

    Python has been installed as

      /usr/local/bin/python3

    Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to

    `python3`, `python3-config`, `pip3` etc., respectively, have been installed into

      /usr/local/opt/python/libexec/bin

    If you need Homebrew’s Python 2.7 run

      brew install python@2

    You can install Python packages with

      pip3 install <package>

    They will install into the site-package directory

      /usr/local/lib/python3.7/site-packages

    See: https://docs.brew.sh/Homebrew-and-Python

python 抓取unsplash图片

import urllib
image_url = [
‘https://images.unsplash.com/photo-1565099824688-e93eb20fe622?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjExMDk0fQ&auto=format&fit=crop&w=751&q=80’,
‘https://images.unsplash.com/photo-1565014312097-24e9c95b0bcc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80’,
‘https://images.unsplash.com/photo-1565098235617-1d429c400daa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80’
]
i=1
for item in image_url:
urllib.urlretrieve(item,”%s.jpg”%(i))
i+=1

 

Python读取网页内容

import urllib
url=’http://www.feinfood.com/restaurants/%E5%BE%B7%E5%9B%BD/%E6%85%95%E5%B0%BC%E9%BB%91.xhtml?p=&key=&o=0&pg=1′
response = urllib.urlopen(url=url)
print(response.read().decode(‘utf-8’));