時候不早了,廢話也不多說了。開始吧!
1. paster create --template=pylons minispider
2. MySQL,建立數據庫minispider

CREATE ? TABLE ?minispider.titleinfo
(?id?
INTEGER ?UNSIGNED? NOT ? NULL ?AUTO_INCREMENT,
??link?
VARCHAR ( 255 )? NOT ? NULL ? DEFAULT ? '' ,
??description?
VARCHAR ( 255 )? NOT ? NULL ? DEFAULT ? '' ,
??sitename?
VARCHAR ( 255 )? NOT ? NULL ? DEFAULT ? '' ,
??updatetime?
TIMESTAMP ? NOT ? NULL ? DEFAULT ? 0 ,
??
PRIMARY ? KEY (id)
)

3. The Model
1) 修改development.ini,代碼如下:
?1#
?2#?minispider?-?Pylons?development?environment?configuration
?3#
?4#?The?%(here)s?variable?will?be?replaced?with?the?parent?directory?of?this?file
?5#
?6[DEFAULT]
?7debug?=?true
?8email_to?=?you@yourdomain.com
?9smtp_server?=?localhost
10error_email_from?=?paste@localhost
11
12[server:main]
13use?=?egg:Paste#http
14host?=?0.0.0.0
15port?=?5000
16
17[app:main]
18use?=?egg:minispider
19cache_dir?=?%(here)s/data
20session_key?=?minispider
21session_secret?=?somesecret
22
23#?If?you'd?like?to?fine-tune?the?individual?locations?of?the?cache?data?dirs
24#?for?Myghty,?the?Cache?data,?or?the?Session?saves,?un-comment?the?desired
25#?settings?here:
26#myghty_data_dir?=?%(here)s/data/templates
27#cache_data_dir?=?%(here)s/data/cache
28#session_data_dir?=?%(here)s/data/sessions
29
30#?Specify?the?database?for?SQLObject?to?use?via?pylons.database.PackageHub.
31#?%(here)?may?include?a?':'?character?on?Windows?environments;?this?can
32#?invalidate?the?URI?when?specifying?a?SQLite?db?via?path?name.?Refer?to?the
33#?SQLObject?documentation?for?a?special?syntax?to?preserve?the?URI.
34#sqlobject.dburi?=?sqlite:%(here)s/somedb.db
35sqlobject.dburi?=?mysql://root:123456@localhost:3306/minispider
36
37#?WARNING:?*THE?LINE?BELOW?MUST?BE?UNCOMMENTED?ON?A?PRODUCTION?ENVIRONMENT*
38#?Debug?mode?will?enable?the?interactive?debugging?tool,?allowing?ANYONE?to
39#?execute?malicious?code?after?an?exception?is?raised.
40#set?debug?=?false
第35行為添加的部分。
2)在models目錄下,建立msmodel.py,代碼如下:
from?sqlobject?import?*
from?pylons.database?import?PackageHub
hub?
=?PackageHub("minispider")
__connection__?=?hub

class?titleinfo(SQLObject):
????link?
=?StringCol(length=255)
????description?
=?StringCol(length=255)
????sitename?
=?StringCol(length=255)
????updatetime?
=?DateTimeCol()
修改__init__.py,代碼如下:
##?NOTE
#
#???If?you?plan?on?using?SQLObject,?the?following?should?be?un-commented?and?provides
#
#???a?starting?point?for?setting?up?your?schema

#from?sqlobject?import?*
#
from?pylons.database?import?PackageHub
#
hub?=?PackageHub("minispider")
#
__connection__?=?hub

#?You?should?then?import?your?SQLObject?classes
#
?from?myclass?import?MyDataClass
from?msmodel?import?titleinfo

4.The view
在templates文件夾下建立ms文件夾,在ms文件中建立list.myt,代碼如下:
<html>
<head>
<title>Generated?by?Mini?Spider?v0.1</title>
</head>
<body>
<table?width="80%"??border="0">
??
<tr>
????
<td?width="60%"><strong>What</strong></td>
????
<td?width="20%"><strong>Where</strong></td>
????
<td?width="20%"><strong>When</strong></td>
??
</tr>
%?for?ti?in?c.titleinfo:
??
<tr>
????
<td><a?href="<%?ti.link?%>"?target="_blank"><%?ti.description?%></a></td>
????
<td><%?ti.sitename?%></td>
????
<td><%?ti.updatetime?%></td>
??
</tr>
%?#endfor
</table>
</body>
</html>
務必注意代碼的縮進!浪費了偶半個多小時!

5.The controller
命令行運行:
cd minispider
paster controller ms
將controllers文件夾下ms.py修改,代碼如下:
from?minispider.lib.base?import?*

class?MsController(BaseController):
????????
??template_prefix?
=?'/ms'
????????
??
def?index(self):
????redirect_to(action
='list')
????????
??
def?list(self):
????c.titleinfo?
=?list(model.titleinfo.select())
????
return?render_response(self.template_prefix?+?'/list.myt')

6. run
命令行運行:
paster serve --reload development.ini
ok,訪問:http://127.0.0.1:5000/ms
結果類似:
WhatWhereWhen
BaiduBaidu2006-12-05 22:18:12
GoogleGoogle2006-12-05 22:18:42

初試,功能之強大,操作之簡便,初見端倪。

歡迎大家訪問我的個人網站 萌萌的IT人