crawlo 1.0.5__py3-none-any.whl → 1.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of crawlo might be problematic. Click here for more details.
- crawlo/__version__.py +1 -1
- crawlo/cli.py +41 -0
- crawlo/commands/__init__.py +10 -0
- crawlo/commands/genspider.py +111 -0
- crawlo/commands/run.py +149 -0
- crawlo/commands/startproject.py +101 -0
- crawlo/crawler.py +1 -206
- crawlo/exceptions.py +5 -0
- crawlo/items/__init__.py +18 -58
- crawlo/items/base.py +31 -0
- crawlo/items/fields.py +54 -0
- crawlo/items/items.py +10 -20
- crawlo/settings/default_settings.py +1 -1
- crawlo/templates/crawlo.cfg.tmpl +11 -0
- crawlo/templates/project/__init__.py.tmpl +4 -0
- crawlo/templates/project/items.py.tmpl +18 -0
- crawlo/templates/project/middlewares.py.tmpl +76 -0
- crawlo/templates/project/pipelines.py.tmpl +64 -0
- crawlo/templates/project/settings.py.tmpl +54 -0
- crawlo/templates/project/spiders/__init__.py.tmpl +6 -0
- crawlo/templates/spider/spider.py.tmpl +32 -0
- crawlo/utils/project.py +159 -19
- crawlo/utils/spider_loader.py +63 -0
- {crawlo-1.0.5.dist-info → crawlo-1.0.6.dist-info}/METADATA +1 -1
- {crawlo-1.0.5.dist-info → crawlo-1.0.6.dist-info}/RECORD +32 -22
- crawlo-1.0.6.dist-info/entry_points.txt +2 -0
- examples/gxb/items.py +1 -1
- examples/gxb/run.py +2 -1
- examples/gxb/settings.py +2 -1
- examples/gxb/spider/{telecom_device_licenses.py → telecom_device.py} +1 -1
- crawlo/templates/item_template.tmpl +0 -22
- crawlo/templates/project_template/items/__init__.py +0 -0
- crawlo/templates/project_template/main.py +0 -33
- crawlo/templates/project_template/setting.py +0 -190
- crawlo/templates/project_template/spiders/__init__.py +0 -0
- crawlo/templates/spider_template.tmpl +0 -31
- crawlo-1.0.5.dist-info/entry_points.txt +0 -2
- {crawlo-1.0.5.dist-info → crawlo-1.0.6.dist-info}/WHEEL +0 -0
- {crawlo-1.0.5.dist-info → crawlo-1.0.6.dist-info}/top_level.txt +0 -0
crawlo/items/__init__.py
CHANGED
|
@@ -1,62 +1,22 @@
|
|
|
1
1
|
#!/usr/bin/python
|
|
2
|
-
# -*- coding:UTF-8 -*-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
crawlo.items 包
|
|
5
|
+
===============
|
|
6
|
+
提供 Item 和 Field 类用于数据定义和验证。
|
|
7
|
+
"""
|
|
8
|
+
from .fields import Field
|
|
9
|
+
from .items import Item
|
|
10
|
+
from .base import ItemMeta
|
|
11
|
+
from crawlo.exceptions import ItemInitError, ItemAttributeError
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
'Item',
|
|
15
|
+
'Field',
|
|
16
|
+
'ItemMeta',
|
|
17
|
+
'ItemInitError',
|
|
18
|
+
'ItemAttributeError'
|
|
19
|
+
]
|
|
5
20
|
|
|
6
21
|
|
|
7
|
-
class Field:
|
|
8
|
-
def __init__(
|
|
9
|
-
self,
|
|
10
|
-
nullable: bool = True,
|
|
11
|
-
*,
|
|
12
|
-
default: Any = None,
|
|
13
|
-
field_type: Optional[Type] = None,
|
|
14
|
-
max_length: Optional[int] = None,
|
|
15
|
-
description: str = ""
|
|
16
|
-
):
|
|
17
|
-
self.nullable = nullable
|
|
18
|
-
self.default = default
|
|
19
|
-
self.field_type = field_type
|
|
20
|
-
self.max_length = max_length
|
|
21
|
-
self.description = description
|
|
22
22
|
|
|
23
|
-
def validate(self, value: Any, field_name: str = "") -> Any:
|
|
24
|
-
if value is None or (isinstance(value, str) and value.strip() == ""):
|
|
25
|
-
if self.default is not None:
|
|
26
|
-
return self.default
|
|
27
|
-
elif not self.nullable:
|
|
28
|
-
raise ValueError(
|
|
29
|
-
f"字段 '{field_name}' 不允许为空。"
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
if value is not None and not (isinstance(value, str) and value.strip() == ""):
|
|
33
|
-
if self.field_type and not isinstance(value, self.field_type):
|
|
34
|
-
raise TypeError(
|
|
35
|
-
f"字段 '{field_name}' 类型错误:期望类型 {self.field_type}, 得到 {type(value)},值:{value!r}"
|
|
36
|
-
)
|
|
37
|
-
if self.max_length and len(str(value)) > self.max_length:
|
|
38
|
-
raise ValueError(
|
|
39
|
-
f"字段 '{field_name}' 长度超限:最大长度 {self.max_length},当前长度 {len(str(value))},值:{value!r}"
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
return value
|
|
43
|
-
|
|
44
|
-
def __repr__(self):
|
|
45
|
-
return f"<Field required={self.nullable} type={self.field_type} default={self.default}>"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
class ItemMeta(ABCMeta):
|
|
49
|
-
"""
|
|
50
|
-
元类
|
|
51
|
-
"""
|
|
52
|
-
def __new__(mcs, name, bases, attrs):
|
|
53
|
-
field = {}
|
|
54
|
-
cls_attr = {}
|
|
55
|
-
for k, v in attrs.items():
|
|
56
|
-
if isinstance(v, Field):
|
|
57
|
-
field[k] = v
|
|
58
|
-
else:
|
|
59
|
-
cls_attr[k] = v
|
|
60
|
-
cls_instance = super().__new__(mcs, name, bases, attrs)
|
|
61
|
-
cls_instance.FIELDS = field
|
|
62
|
-
return cls_instance
|
crawlo/items/base.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
基础元类定义
|
|
5
|
+
"""
|
|
6
|
+
from abc import ABCMeta
|
|
7
|
+
|
|
8
|
+
from crawlo.items import Field
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ItemMeta(ABCMeta):
|
|
12
|
+
"""
|
|
13
|
+
元类,用于自动收集 Item 类中的 Field 定义
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __new__(mcs, name, bases, attrs):
|
|
17
|
+
fields = {}
|
|
18
|
+
cls_attrs = {}
|
|
19
|
+
|
|
20
|
+
# 收集所有 Field 实例
|
|
21
|
+
for attr_name, attr_value in attrs.items():
|
|
22
|
+
if isinstance(attr_value, Field):
|
|
23
|
+
fields[attr_name] = attr_value
|
|
24
|
+
else:
|
|
25
|
+
cls_attrs[attr_name] = attr_value
|
|
26
|
+
|
|
27
|
+
# 创建类实例
|
|
28
|
+
cls_instance = super().__new__(mcs, name, bases, cls_attrs)
|
|
29
|
+
cls_instance.FIELDS = fields
|
|
30
|
+
|
|
31
|
+
return cls_instance
|
crawlo/items/fields.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Field 类定义
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Optional, Type
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Field:
|
|
11
|
+
"""
|
|
12
|
+
字段定义类,用于定义 Item 的字段属性和验证规则
|
|
13
|
+
"""
|
|
14
|
+
def __init__(
|
|
15
|
+
self,
|
|
16
|
+
nullable: bool = True,
|
|
17
|
+
*,
|
|
18
|
+
default: Any = None,
|
|
19
|
+
field_type: Optional[Type] = None,
|
|
20
|
+
max_length: Optional[int] = None,
|
|
21
|
+
description: str = ""
|
|
22
|
+
):
|
|
23
|
+
self.nullable = nullable
|
|
24
|
+
self.default = default
|
|
25
|
+
self.field_type = field_type
|
|
26
|
+
self.max_length = max_length
|
|
27
|
+
self.description = description
|
|
28
|
+
|
|
29
|
+
def validate(self, value: Any, field_name: str = "") -> Any:
|
|
30
|
+
"""
|
|
31
|
+
验证字段值是否符合规则
|
|
32
|
+
"""
|
|
33
|
+
if value is None or (isinstance(value, str) and value.strip() == ""):
|
|
34
|
+
if self.default is not None:
|
|
35
|
+
return self.default
|
|
36
|
+
elif not self.nullable:
|
|
37
|
+
raise ValueError(
|
|
38
|
+
f"字段 '{field_name}' 不允许为空。"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if value is not None and not (isinstance(value, str) and value.strip() == ""):
|
|
42
|
+
if self.field_type and not isinstance(value, self.field_type):
|
|
43
|
+
raise TypeError(
|
|
44
|
+
f"字段 '{field_name}' 类型错误:期望类型 {self.field_type}, 得到 {type(value)},值:{value!r}"
|
|
45
|
+
)
|
|
46
|
+
if self.max_length and len(str(value)) > self.max_length:
|
|
47
|
+
raise ValueError(
|
|
48
|
+
f"字段 '{field_name}' 长度超限:最大长度 {self.max_length},当前长度 {len(str(value))},值:{value!r}"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
return value
|
|
52
|
+
|
|
53
|
+
def __repr__(self):
|
|
54
|
+
return f"<Field nullable={self.nullable} type={self.field_type} default={self.default}>"
|
crawlo/items/items.py
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/python
|
|
2
2
|
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Item 类定义
|
|
5
|
+
"""
|
|
3
6
|
from copy import deepcopy
|
|
4
7
|
from pprint import pformat
|
|
5
8
|
from typing import Any, Iterator, Dict
|
|
6
9
|
from collections.abc import MutableMapping
|
|
7
10
|
|
|
8
|
-
from
|
|
11
|
+
from .base import ItemMeta
|
|
9
12
|
from crawlo.exceptions import ItemInitError, ItemAttributeError
|
|
10
13
|
|
|
11
14
|
|
|
12
15
|
class Item(MutableMapping, metaclass=ItemMeta):
|
|
16
|
+
"""
|
|
17
|
+
数据项基类,用于定义结构化数据
|
|
18
|
+
"""
|
|
13
19
|
FIELDS: Dict[str, Any] = {}
|
|
14
20
|
|
|
15
21
|
def __init__(self, *args, **kwargs):
|
|
16
22
|
if args:
|
|
17
23
|
raise ItemInitError(f"{self.__class__.__name__} 不支持位置参数:{args},请使用关键字参数初始化。")
|
|
18
|
-
if kwargs:
|
|
19
|
-
for key, value in kwargs.items():
|
|
20
|
-
self[key] = value
|
|
21
24
|
|
|
22
25
|
self._values: Dict[str, Any] = {}
|
|
23
26
|
|
|
@@ -66,14 +69,12 @@ class Item(MutableMapping, metaclass=ItemMeta):
|
|
|
66
69
|
super().__setattr__(key, value)
|
|
67
70
|
|
|
68
71
|
def __getattr__(self, item: str) -> Any:
|
|
69
|
-
# 当获取不到属性时触发
|
|
70
72
|
raise AttributeError(
|
|
71
73
|
f"{self.__class__.__name__} 不支持字段:{item}。"
|
|
72
74
|
f"请先在 `{self.__class__.__name__}` 中声明该字段,再通过 item[{item!r}] 获取。"
|
|
73
75
|
)
|
|
74
76
|
|
|
75
77
|
def __getattribute__(self, item: str) -> Any:
|
|
76
|
-
# 属性拦截器,只要访问属性就会进入该方法
|
|
77
78
|
try:
|
|
78
79
|
field = super().__getattribute__("FIELDS")
|
|
79
80
|
if isinstance(field, dict) and item in field:
|
|
@@ -96,20 +97,9 @@ class Item(MutableMapping, metaclass=ItemMeta):
|
|
|
96
97
|
return len(self._values)
|
|
97
98
|
|
|
98
99
|
def to_dict(self) -> Dict[str, Any]:
|
|
100
|
+
"""转换为字典"""
|
|
99
101
|
return dict(self)
|
|
100
102
|
|
|
101
103
|
def copy(self) -> "Item":
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if __name__ == '__main__':
|
|
106
|
-
class TestItem(Item):
|
|
107
|
-
url = Field(nullable=False, field_type=str, max_length=100)
|
|
108
|
-
title = Field(default="无标题", field_type=str)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
test_item = TestItem()
|
|
112
|
-
test_item['title'] = '百度首页'
|
|
113
|
-
test_item['url'] = 'hhh'
|
|
114
|
-
# test_item.title = 'fffff'
|
|
115
|
-
print(test_item)
|
|
104
|
+
"""深拷贝当前 Item"""
|
|
105
|
+
return deepcopy(self)
|
|
@@ -137,7 +137,7 @@ LOG_ENCODING = 'utf-8'
|
|
|
137
137
|
|
|
138
138
|
# ============================== 代理配置 ==============================
|
|
139
139
|
|
|
140
|
-
PROXY_ENABLED =
|
|
140
|
+
PROXY_ENABLED = False # 是否启用代理
|
|
141
141
|
PROXY_API_URL = "https://api.proxyprovider.com/get" # 代理获取接口(请替换为真实地址)
|
|
142
142
|
|
|
143
143
|
# 代理提取方式(支持字段路径或函数)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
{{project_name}}.items
|
|
4
|
+
======================
|
|
5
|
+
定义你抓取的数据结构。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from crawlo.items import Item, Field
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ExampleItem(Item):
|
|
12
|
+
"""
|
|
13
|
+
一个示例数据项。
|
|
14
|
+
"""
|
|
15
|
+
# name = Field()
|
|
16
|
+
# price = Field()
|
|
17
|
+
# description = Field()
|
|
18
|
+
pass
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
{{project_name}}.middlewares
|
|
4
|
+
============================
|
|
5
|
+
自定义中间件,用于在请求/响应/异常处理过程中插入自定义逻辑。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
# 示例:下载器中间件
|
|
9
|
+
class CustomDownloaderMiddleware:
|
|
10
|
+
"""
|
|
11
|
+
下载器中间件示例。
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def process_request(self, request, spider):
|
|
15
|
+
"""
|
|
16
|
+
在请求被下载器执行前调用。
|
|
17
|
+
"""
|
|
18
|
+
# request.headers['User-Agent'] = 'Custom UA'
|
|
19
|
+
# return None # 继续处理
|
|
20
|
+
# return request # 修改并返回
|
|
21
|
+
# return Response(...) # 返回一个响应,停止下载
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
def process_response(self, request, response, spider):
|
|
25
|
+
"""
|
|
26
|
+
在响应被 Spider 处理前调用。
|
|
27
|
+
"""
|
|
28
|
+
# return response # 继续处理
|
|
29
|
+
# return request # 重试请求
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
def process_exception(self, request, exception, spider):
|
|
33
|
+
"""
|
|
34
|
+
在下载或处理过程中发生异常时调用。
|
|
35
|
+
"""
|
|
36
|
+
# return None # 继续抛出异常
|
|
37
|
+
# return request # 重试
|
|
38
|
+
# return Response(...) # 返回一个响应
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# 示例:Spider 中间件
|
|
43
|
+
class CustomSpiderMiddleware:
|
|
44
|
+
"""
|
|
45
|
+
Spider 中间件示例。
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def process_spider_input(self, response, spider):
|
|
49
|
+
"""
|
|
50
|
+
在 Spider 的 parse 方法被调用前调用。
|
|
51
|
+
"""
|
|
52
|
+
# 可以用来验证响应
|
|
53
|
+
pass
|
|
54
|
+
|
|
55
|
+
def process_spider_output(self, response, result, spider):
|
|
56
|
+
"""
|
|
57
|
+
在 Spider 的 parse 方法返回结果后调用。
|
|
58
|
+
"""
|
|
59
|
+
# 可以用来过滤或修改结果
|
|
60
|
+
# for item in result:
|
|
61
|
+
# yield item
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
def process_spider_exception(self, response, exception, spider):
|
|
65
|
+
"""
|
|
66
|
+
在 Spider 的 parse 方法抛出异常时调用。
|
|
67
|
+
"""
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
def process_start_requests(self, start_requests, spider):
|
|
71
|
+
"""
|
|
72
|
+
在 Spider 的 start_requests 生成器被消费时调用。
|
|
73
|
+
"""
|
|
74
|
+
# for request in start_requests:
|
|
75
|
+
# yield request
|
|
76
|
+
pass
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
{{project_name}}.pipelines
|
|
4
|
+
==========================
|
|
5
|
+
数据管道,用于处理 Spider 返回的 Item。
|
|
6
|
+
例如:清理、验证、去重、保存到数据库等。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
class PrintItemPipeline:
|
|
10
|
+
"""
|
|
11
|
+
一个简单的管道,用于打印 Item。
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def process_item(self, item, spider):
|
|
15
|
+
print(f"Pipeline received item: {dict(item)}")
|
|
16
|
+
return item
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class DuplicatesPipeline:
|
|
20
|
+
"""
|
|
21
|
+
一个去重管道示例。
|
|
22
|
+
"""
|
|
23
|
+
def __init__(self):
|
|
24
|
+
self.seen = set()
|
|
25
|
+
|
|
26
|
+
def process_item(self, item, spider):
|
|
27
|
+
identifier = item.get('id') or item.get('url')
|
|
28
|
+
if identifier in self.seen:
|
|
29
|
+
spider.logger.debug(f"Duplicate item found: {identifier}")
|
|
30
|
+
raise DropItem(f"Duplicate item: {identifier}")
|
|
31
|
+
self.seen.add(identifier)
|
|
32
|
+
return item
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# class MySQLPipeline:
|
|
36
|
+
# """
|
|
37
|
+
# 将 Item 保存到 MySQL 的管道示例。
|
|
38
|
+
# """
|
|
39
|
+
# def __init__(self, mysql_uri, mysql_user, mysql_password, mysql_db):
|
|
40
|
+
# self.mysql_uri = mysql_uri
|
|
41
|
+
# self.mysql_user = mysql_user
|
|
42
|
+
# self.mysql_password = mysql_password
|
|
43
|
+
# self.mysql_db = mysql_db
|
|
44
|
+
# self.connection = None
|
|
45
|
+
#
|
|
46
|
+
# @classmethod
|
|
47
|
+
# def from_settings(cls, settings):
|
|
48
|
+
# return cls(
|
|
49
|
+
# mysql_uri=settings.get('MYSQL_HOST'),
|
|
50
|
+
# mysql_user=settings.get('MYSQL_USER'),
|
|
51
|
+
# mysql_password=settings.get('MYSQL_PASSWORD'),
|
|
52
|
+
# mysql_db=settings.get('MYSQL_DB')
|
|
53
|
+
# )
|
|
54
|
+
#
|
|
55
|
+
# def open_spider(self, spider):
|
|
56
|
+
# self.connection = pymysql.connect(...)
|
|
57
|
+
#
|
|
58
|
+
# def close_spider(self, spider):
|
|
59
|
+
# if self.connection:
|
|
60
|
+
# self.connection.close()
|
|
61
|
+
#
|
|
62
|
+
# def process_item(self, item, spider):
|
|
63
|
+
# # 执行 SQL 插入
|
|
64
|
+
# return item
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""自动创建的 settings.py 文件"""
|
|
3
|
+
|
|
4
|
+
PROJECT_NAME = '{{project_name}}'
|
|
5
|
+
VERSION = '1.0'
|
|
6
|
+
|
|
7
|
+
# ============================== 网络请求配置 ==============================
|
|
8
|
+
DOWNLOADER = "crawlo.downloader.aiohttp_downloader.AioHttpDownloader"
|
|
9
|
+
DOWNLOAD_TIMEOUT = 60
|
|
10
|
+
VERIFY_SSL = True
|
|
11
|
+
USE_SESSION = True
|
|
12
|
+
|
|
13
|
+
DOWNLOAD_DELAY = 1.0
|
|
14
|
+
RANDOMNESS = True
|
|
15
|
+
|
|
16
|
+
MAX_RETRY_TIMES = 3
|
|
17
|
+
RETRY_HTTP_CODES = [408, 429, 500, 502, 503, 504, 522, 524]
|
|
18
|
+
IGNORE_HTTP_CODES = [403, 404]
|
|
19
|
+
|
|
20
|
+
CONNECTION_POOL_LIMIT = 100
|
|
21
|
+
|
|
22
|
+
# ============================== 并发与调度 ==============================
|
|
23
|
+
CONCURRENCY = 8
|
|
24
|
+
MAX_RUNNING_SPIDERS = 3
|
|
25
|
+
|
|
26
|
+
# ============================== 数据存储 ==============================
|
|
27
|
+
MYSQL_HOST = '127.0.0.1'
|
|
28
|
+
MYSQL_PORT = 3306
|
|
29
|
+
MYSQL_USER = 'root'
|
|
30
|
+
MYSQL_PASSWORD = '123456'
|
|
31
|
+
MYSQL_DB = '{{project_name}}'
|
|
32
|
+
MYSQL_TABLE = 'crawled_data'
|
|
33
|
+
|
|
34
|
+
# ============================== 去重过滤 ==============================
|
|
35
|
+
FILTER_CLASS = 'crawlo.filters.memory_filter.MemoryFilter'
|
|
36
|
+
|
|
37
|
+
# ============================== 中间件 & 管道 ==============================
|
|
38
|
+
MIDDLEWARES = [
|
|
39
|
+
'crawlo.middleware.request_ignore.RequestIgnoreMiddleware',
|
|
40
|
+
'crawlo.middleware.download_delay.DownloadDelayMiddleware',
|
|
41
|
+
'crawlo.middleware.default_header.DefaultHeaderMiddleware',
|
|
42
|
+
'crawlo.middleware.proxy.ProxyMiddleware',
|
|
43
|
+
'crawlo.middleware.retry.RetryMiddleware',
|
|
44
|
+
'crawlo.middleware.response_code.ResponseCodeMiddleware',
|
|
45
|
+
'crawlo.middleware.response_filter.ResponseFilterMiddleware',
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
PIPELINES = [
|
|
49
|
+
'crawlo.pipelines.console_pipeline.ConsolePipeline',
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
# ============================== 日志 ==============================
|
|
53
|
+
LOG_LEVEL = 'INFO'
|
|
54
|
+
LOG_FILE = f'logs/{{{{project_name}}}}.log'
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
{{project_name}}.spiders.{{spider_name}}
|
|
4
|
+
=======================================
|
|
5
|
+
由 `crawlo genspider` 命令生成的爬虫。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from crawlo.spider import Spider
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class {{class_name}}(Spider):
|
|
12
|
+
"""
|
|
13
|
+
爬虫:{{spider_name}}
|
|
14
|
+
"""
|
|
15
|
+
name = '{{spider_name}}'
|
|
16
|
+
allowed_domains = ['{{domain}}']
|
|
17
|
+
start_urls = ['https://{{domain}}/']
|
|
18
|
+
|
|
19
|
+
def parse(self, response):
|
|
20
|
+
"""
|
|
21
|
+
解析响应的主方法。
|
|
22
|
+
"""
|
|
23
|
+
# TODO: 在这里编写你的解析逻辑
|
|
24
|
+
|
|
25
|
+
# 示例:提取数据
|
|
26
|
+
# item = {{item_class}}()
|
|
27
|
+
# item['title'] = response.xpath('//title/text()').get()
|
|
28
|
+
# yield item
|
|
29
|
+
|
|
30
|
+
# 示例:提取链接并跟进
|
|
31
|
+
# for href in response.xpath('//a/@href').getall():
|
|
32
|
+
# yield response.follow(href, callback=self.parse)
|