为了提高模块加载的速度,每个模块都会在__pycache__文件夹中放置该模块的预编译模块,命名为,version是模块的预编译版本编码,一般都包含Python的版本号。例如在CPython 发行版中,文件的预编译文件就是:__pycache__/-。这种命名规则可以保证不同版本的模块和不同版本的python编译器的预编译模块可以共存。以下是小编为你整理的python3+网络编程入门
脚本在运行之前会首先检查python文件的最后编辑日期和预编译模块的编译时间,从而决定是否需要重新编译。预编译模块也是跨平台的,所以不同的模块是可以在不同的系统和不同的架构之间共享的。
Python在两种情况下不检查缓存。第一种,从命令行中直接加载的模块总是会重新编译并且结果不保存。第二种,如果没有源模块,则不会检查缓存。为了支持无源代码的部署方式,应该将预编译模块放在源代码文件夹中而不是__pycache__中,并且不要包含源代码模块。
[图片0]
你可以使用-O和-OO参数来降低预编译模块的大小。-O开关会去除assert语句,-OO开关会去除assert语句和__doc__字符串。因为有些模块要依赖这些语句,因此只有当你确认模块的内容时才去使用这些开关。优化模块的后缀名是.pyo。
.pyo和.pyc文件的执行速度不会比.py文件快,快的地方在于模块加载的速度。compileall模块可以用来把某个文件夹的中的所有文件都编译成为.pyc或者.pyo文件。
lambda通常是匿名函数的代名词,我们用到lambda的时候就是创建一个匿名函数:举个简单的例子:x代表了输入,x**2代表计算方法,也代表返回内容,也就是说这个函数输入一个数,返回这个数的平方。但是因为这个函数没有函数名,所以无法在其他地方调用
除非我们将这个函数起一个名字:但通常我们不会这么做,匿名函数只是作为匿名使用。
filter函数用法是:filter(fuction,list):将list中每一个元素带入到function中,计算返回值,将返回值为True的list中的元素形成一个新的list,当然也可以是tuple。
对于上面这个函数,我们可以用lambda来简化:它的意思是,如果alist中的值的平方小于5,就返回这个值,形成一个新的list
关于python
Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年。Python是纯粹的自由软件,源代码和解释器CPython遵循GPL(GNU General Public License)协议[1] 。Python语法简洁清晰,特色之一是强制用空白符(w作为语句缩进。
首先从Python官网(/downloads/)下载Python安装程序,本文以Python 为例:
[图片1]
web-based installer 需要通过联网完成安装2、executable installer 通过可执行文件(*.exe)方式安装3、embeddable zip file 嵌入式版本,可以集成到其它应用中
笔者开发环境是Windows所以选择红框选中的版本,Mac用户下载红框标注的版本,下载完成后,找到相应的软件包,
#coding=utf-8
import sys
reload(sys)
#python默认环境编码时ascii
("utf-8")
from import baseSpider
from import Request
from import HtmlXPathSelector
from import TutorialItem
import re
class DoubanSpider(baseSpider):
name = "douban"
allowed_domains = [""]
start_urls = []
def start_requests(self):
file_object = open('','r')
try:
url_head = "/subject_search?search_text="
for line in file_object:
(url_head + line)
for url in :
yield (url)
finally:
()
#()
def parse(self, response):
#open("",'wb').write()
hxs = HtmlXPathSelector(response)
#movie_name = ('//*[@id="content"]/div/div[1]/div[2]/table[1]/tr/td[1]/a/@title').extract()
movie_link = ('//*[@id="content"]/div/div[1]/div[2]/table[1]/tr/td[1]/a/@href').extract()
#movie_desc = ('//*[@id="content"]/div/div[1]/div[2]/table[1]/tr/td[2]/div/p/text()').extract()
if movie_link:
yield Request(movie_link[0],callback=)
def parse_item(self,response):
hxs = HtmlXPathSelector(response)
movie_name = ('//*[@id="content"]/h1/span[1]/text()').extract()
movie_director = ('//*[@id="info"]/span[1]/span[2]/a/text()').extract()
movie_writer = ('//*[@id="info"]/span[2]/span[2]/a/text()').extract()
#爬取电影详情需要在已有对象中继续爬取
movie_description_paths = ('//*[@id="link-report"]')
movie_description = []
for movie_description_path in movie_description_paths:
movie_description = ('.//*[@property="v:summary"]/text()').extract()
#提取演员需要从已有的xPath对象中继续爬我要的内容
movie_roles_paths = ('//*[@id="info"]/span[3]/span[2]')
movie_roles = []
for movie_roles_path in movie_roles_paths:
movie_roles = ('.//*[@rel="v:starring"]/text()').extract()
#获取电影详细信息序列
movie_detail = ('//*[@id="info"]').extract()
item = TutorialItem()
item['movie_name'] = ''.join(movie_name).strip().replace(',',';').replace(''','\'').replace('"','\"').replace(':',';')
#item['movie_link'] = movie_link[0]
item['movie_director'] = movie_director[0].strip().replace(',',';').replace(''','\'').replace('"','\"').replace(':',';') if len(movie_director) > 0 else ''
#由于逗号是拿来分割电影所有信息的,所以需要处理逗号;引号也要处理,否则插入数据库会有问题
item['movie_description'] = movie_description[0].strip().replace(',',';').replace(''','\'').replace('"','\"').replace(':',';') if len(movie_description) > 0 else ''
item['movie_writer'] = ';'.join(movie_writer).strip().replace(',',';').replace(''','\'').replace('"','\"').replace(':',';')
item['movie_roles'] = ';'.join(movie_roles).strip().replace(',',';').replace(''','\'').replace('"','\"').replace(':',';')
#item['movie_language'] = movie_language[0].strip() if len(movie_language) > 0 else ''
#item['movie_date'] = ''.join(movie_date).strip()
#item['movie_long'] = ''.join(movie_long).strip()
#电影详情信息字符串
movie_detail_str = ''.join(movie_detail).strip()
#print movie_detail_str
movie_language_str = ".*语言: (.+?)
*".decode("utf8")
movie_date_str = ".*上映日期: (S+?).*".decode("utf8")
movie_long_str = ".*片长:
pattern_language =(movie_language_str,)
pattern_date = (movie_date_str,)
pattern_long = (movie_long_str,)
movie_language = (pattern_language,movie_detail_str)
movie_date = (pattern_date,movie_detail_str)
movie_long = (pattern_long,movie_detail_str)
item['movie_language'] = ""
if movie_language:
item['movie_language'] = (1).strip().replace(',',';').replace(''','\'').replace('"','\"').replace(':',';')
#item['movie_detail'] = ''.join(movie_detail).strip()
item['movie_date'] = ""
if movie_date:
item['movie_date'] = (1).strip().replace(',',';').replace(''','\'').replace('"','\"').replace(':',';')
item['movie_long'] = ""
if movie_long:
item['movie_long'] = (1)
yield item