确保安装依赖存在sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make 下载python3并解压wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz tar -xvJf Python-3.6.2.tar.xz 编译安装python3cd Python-3.6.2 ./configure prefix=/usr/local/python3 sudo make sudo make install 安装完/usr/local/ 下就有python3文件夹将原来python改名为python2sudo mv /usr/bin/python /usr/bin/python2 修改一些依赖python2的文件sudo nano /usr/bin/yum 将头部 #! /usr/bin/python修改为#! /usr/bin/python2 同样的编辑 /usr/libexec/urlgrabber-ext-down文件 sudo nano /usr/libexec/urlgrabber-ext-down 将头部 #! /usr/bin/python修改为#! /usr/bin/python2 添加软链到执行目录ln -s /usr/local/python3/bin/python3 /usr/bin/python ln -s /usr/local/python3/bin/python3 /usr/bin/python3 加入python3环境变量sudo nano /etc/profile export PYTHON3_HOME=/usr/local/python3 export PATH=${PYTHON3_HOME}/bin:$PATH //立即生效 source /etc/profile
安装执行python3，重新安装pip模块python3 -m pip install --upgrade pip --force-reinstall 执行python2，重新安装pip模块python2 -m pip install --upgrade pip --force-reinstall 测试pip2 -V pip3 -V 使用pip2 install xxx pip3 install xxx
#!/usr/bin/env python# -*- encoding: utf-8 -*- # Created on 2017-11-30 15:46:23 # Project: ttwanda_3 from pyspider.libs.base_handler import * import re import json from pyspider.libs.utils import md5string class Handler(BaseHandler): crawl_config = { } @every(minutes=24 * 60) def on_start(self): self.crawl('http://www.ttwanda.com', callback=self.index_page) @config(age=10 * 24 * 60 * 60) def index_page(self, response): for each in response.doc('a[href^="http"]').items(): if re.match(u"http://www.ttwanda.com/film/page/\d+|http://www.ttwanda.com/film$", each.attr.href): self.result={} self.crawl(each.attr.href, callback=self.film_list_page, save=self.result) def film_list_page(self, response): for each in response.doc('article.u-movie').items(): self.result = response.save self.result['poster'] = each('img').attr['data-original'] self.result['star'] = each('.pingfen').text() self.crawl(each('.list-poster a[href^="http"]').attr.href, callback=self.film_detail_page, save=self.result,priority=1) self.crawl(response.doc('.next-page a').attr('href'), callback=self.index_page) def film_detail_page(self, response): self.result = response.save for each in response.doc('.mplay-list a').items(): self.crawl(each.attr.href, callback=self.film_video_page, save=self.result) def film_video_page(self, response): self.result = response.save self.result['title'] = self.response.doc('.player_box>strong').text() self.result['url'] = self.response.url #print(self.get_taskid(self.task)) for each in response.doc('script').items(): self.search = re.search(r'var play_type="(\w+)",vid="(\w+)";',each.text()) if self.search: self.result['vtype'] = self.search.group(1) self.result['vid'] = self.search.group(2) return self.result