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]
7
debug
=
true
8
email_to
=
you@yourdomain.com
9
smtp_server
=
localhost
10
error_email_from
=
paste@localhost
11
12
[server:main]
13
use
=
egg:Paste
#
http
14
host
=
0.0
.
0.0
15
port
=
5000
16
17
[app:main]
18
use
=
egg:minispider
19
cache_dir
=
%
(here)s
/
data
20
session_key
=
minispider
21
session_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
35
sqlobject.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
#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]7
debug
=
true8
email_to
=
you@yourdomain.com9
smtp_server
=
localhost10
error_email_from
=
paste@localhost11
12
[server:main]13
use
=
egg:Paste
#
http
14
host
=
0.0
.
0.0
15
port
=
5000
16
17
[app:main]18
use
=
egg:minispider19
cache_dir
=
%
(here)s
/
data20
session_key
=
minispider21
session_secret
=
somesecret22
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
35
sqlobject.dburi
=
mysql:
//
root:
123456
@localhost:
3306
/
minispider36
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
结果类似:
| What | Where | When |
| Baidu | Baidu | 2006-12-05 22:18:12 |
| 2006-12-05 22:18:42 |
初试,功能之强大,操作之简便,初见端倪。
本文介绍如何使用Pylons框架快速搭建一个简单的网页爬虫应用,包括配置数据库、创建模型、视图及控制器等步骤。

被折叠的 条评论
为什么被折叠?



