一. ?安裝python3并設(shè)為默認(rèn)版本枚抵,與python2共存
安裝python3.6可能使用的依賴
# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
1粱挡、下載Python3.6安裝包?解壓、編譯俄精、安裝
新安裝的最新centos7最小化安裝沒有安裝wget,所以要安裝一下
# yum install wget
# wget ?https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
# tar -xzvf Python-3.6.6.tgz?
# cd ?Python-3.6.6
把Python3.6安裝到?/usr/local 目錄,使用make altinstall榕堰,如果使用make install竖慧,在系統(tǒng)中將會有兩個不同版本的Python在/usr/bin/目錄中。這將會導(dǎo)致很多問題
# ./configure --prefix=/usr/local ? ?或者?? ./configure --prefix=/usr/local/python3
如果遇到:configure: error: no acceptable C compiler found in $PATH
解決方法:# yum install gcc
# make
# make install ?或者 ??? make?altinstall
更改/usr/bin/python鏈接 ?創(chuàng)建Python3.6的鏈接,并且設(shè)置python3為默認(rèn)版本(CentOS6.7默認(rèn)安裝的是Python2)
# cd ?/usr/bin
# mv ?python python.backup
# ln -s /usr/local/bin/python3.6 /usr/bin/python
# ln -s /usr/local/bin/python3.6 /usr/bin/python3
更改yum腳本的python依賴
# cd /usr/bin
# ls yum*
# vi /usr/bin/yum
# vi /usr/libexec/urlgrabber-ext-down
因為執(zhí)行yum需要python2版本逆屡,所以我們還要修改yum的配置,? 更改以上文件頭為:
#!/usr/bin/python ? ?改為 ? ?#!/usr/bin/python2
之后python3.6就完成了,能夠同時使用python3.6和python2.7.
setuptools安裝
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
tar -zxvf setuptools-19.6.tar.gz
cd setuptools-19.6
sudo python3 setup.py build
sudo python3 setup.py install
pip3安裝
wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
tar -zxvf pip-8.0.2.tar.gz
cd ?pip-8.0.2
sudo python3 setup.py build
sudo python3 setup.py install
sudo ln -s /usr/local/python3.6/bin/pip3 ? /usr/bin/pip
sudo ln -s /usr/local/python3.6/bin/pip3 ? /usr/bin/pip3
建議把pip和python分別指向pip3和python3圾旨!
二. ?安裝模塊
1. 安裝bs4
wget https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/beautifulsoup4-4.5.1.tar.gz
? ?tar xvzf beautifulsoup4-4.5.1.tar.gz
? ?cd beautifulsoup4-4.5.1?
? ?python setup.py install
2. 安裝requests
? ?wget https://bootstrap.pypa.io/get-pip.py
? ?python get-pip.py?
? ?pip install requests
3. 安裝lxml,依賴有l(wèi)ibxml2-devel魏蔗,libxslt-devel
? ? yum install libxml2-devel libxslt-devel -y
? ? pip install lxml
4. 安裝phantomjs模塊
官網(wǎng):http://phantomjs.org/
wgethttps://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
yum install bzip2? ? ? # 安裝bzip2
tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
mv phantomjs-2.1.1-linux-x86_64 /usr/local/src/phantomjs
ln -sf /usr/local/src/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
yum install fontconfig freetype2
phantomjs -v? ? ? ? # 測試版本號
簡單的python ?selenium 運用
from selenium import webdriver
driver=webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs')
driver.get('')
driver.find_element_by_id("").clear()
driver.find_element_by_id("").send_keys('')
driver.find_element_by_xpath("").click()