ayugespidertools 3.5.2__tar.gz → 3.6.0__tar.gz

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.
Files changed (87) hide show
  1. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/PKG-INFO +1 -1
  2. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/README.md +179 -179
  3. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/mongodbpipe.py +10 -8
  4. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/multiplexing.py +4 -4
  5. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/mysqlerrhandle.py +6 -52
  6. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/typevars.py +1 -32
  7. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/items.py +10 -3
  8. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/pipelines.py +2 -0
  9. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/__init__.py +7 -5
  10. ayugespidertools-3.6.0/ayugespidertools/scraper/pipelines/mysql/__init__.py +129 -0
  11. ayugespidertools-3.6.0/ayugespidertools/scraper/pipelines/mysql/stats.py +168 -0
  12. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mysql/turbo.py +2 -5
  13. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mysql/twisted.py +14 -13
  14. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/spiders/__init__.py +152 -190
  15. ayugespidertools-3.6.0/ayugespidertools/templates/project/module/items.py.tmpl +12 -0
  16. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/settings.py.tmpl +0 -5
  17. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/spiders/async.tmpl +0 -10
  18. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/spiders/basic.tmpl +4 -16
  19. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/spiders/crawl.tmpl +1 -3
  20. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/pyproject.toml +164 -164
  21. ayugespidertools-3.5.2/ayugespidertools/scraper/pipelines/mysql/__init__.py +0 -276
  22. ayugespidertools-3.5.2/ayugespidertools/scraper/pipelines/mysql/stats.py +0 -42
  23. ayugespidertools-3.5.2/ayugespidertools/templates/project/module/items.py.tmpl +0 -30
  24. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/LICENSE +0 -0
  25. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/__init__.py +0 -0
  26. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/commands/__init__.py +0 -0
  27. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/commands/crawl.py +0 -0
  28. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/commands/genspider.py +0 -0
  29. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/commands/startproject.py +0 -0
  30. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/commands/version.py +0 -0
  31. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/__init__.py +0 -0
  32. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/encryption.py +0 -0
  33. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/expend.py +0 -0
  34. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/params.py +0 -0
  35. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/spiderconf.py +0 -0
  36. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/sqlformat.py +0 -0
  37. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/utils.py +0 -0
  38. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/common/yidungap.py +0 -0
  39. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/config.py +0 -0
  40. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/formatdata.py +0 -0
  41. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/imgoperation.py +0 -0
  42. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/middlewares.py +0 -0
  43. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/mongoclient.py +0 -0
  44. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/mysqlclient.py +0 -0
  45. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/oss.py +0 -0
  46. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/request.py +0 -0
  47. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/__init__.py +0 -0
  48. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/http/__init__.py +0 -0
  49. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/http/request/__init__.py +0 -0
  50. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/http/request/form.py +0 -0
  51. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/__init__.py +0 -0
  52. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/headers/__init__.py +0 -0
  53. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/headers/ua.py +0 -0
  54. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/netlib/__init__.py +0 -0
  55. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/netlib/aiohttplib.py +0 -0
  56. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/proxy/__init__.py +0 -0
  57. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/proxy/dynamic.py +0 -0
  58. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/middlewares/proxy/exclusive.py +0 -0
  59. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/download/__init__.py +0 -0
  60. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/download/file.py +0 -0
  61. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mongo/__init__.py +0 -0
  62. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mongo/asynced.py +0 -0
  63. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mongo/fantasy.py +0 -0
  64. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mongo/twisted.py +0 -0
  65. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/msgproducer/__init__.py +0 -0
  66. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/msgproducer/kafkapub.py +0 -0
  67. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/msgproducer/mqpub.py +0 -0
  68. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mysql/asynced.py +0 -0
  69. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/pipelines/mysql/fantasy.py +0 -0
  70. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/scraper/spiders/crawl.py +0 -0
  71. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/spiders.py +0 -0
  72. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/README.md +0 -0
  73. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/VIT/.conf +0 -0
  74. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/__init__.py +0 -0
  75. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/middlewares.py.tmpl +0 -0
  76. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/pipelines.py.tmpl +0 -0
  77. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/run.py.tmpl +0 -0
  78. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/run.sh.tmpl +0 -0
  79. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/module/spiders/__init__.py +0 -0
  80. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/pyproject.toml +0 -0
  81. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/requirements.txt +0 -0
  82. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/project/scrapy.cfg +0 -0
  83. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/spiders/csvfeed.tmpl +0 -0
  84. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/templates/spiders/xmlfeed.tmpl +0 -0
  85. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/utils/__init__.py +0 -0
  86. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/utils/cmdline.py +0 -0
  87. {ayugespidertools-3.5.2 → ayugespidertools-3.6.0}/ayugespidertools/verificationcode.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: AyugeSpiderTools
3
- Version: 3.5.2
3
+ Version: 3.6.0
4
4
  Summary: scrapy 扩展库:用于扩展 Scrapy 功能来解放双手。
5
5
  Home-page: https://www.ayuge.top/mkdocs-material/
6
6
  Keywords: crawler,scraping,twisted,aiohttp,asyncio,scrapy,aiomysql,mmh3
@@ -1,179 +1,179 @@
1
- <p align="center">
2
- <a href="https://ayugespidertools.readthedocs.io" target="_blank" rel="noopener noreferrer">
3
- <img src="./artwork/ayugespidertools-logo.png" alt="ayugespidertools-logo">
4
- </a>
5
- </p>
6
- <hr/>
7
-
8
- # AyugeSpiderTools 介绍
9
-
10
- [![OSCS Status](https://www.oscs1024.com/platform/badge/AyugeSpiderTools.svg?size=small)](https://www.murphysec.com/accept?code=0ec375759aebea7fd260248910b98806&type=1&from=2)
11
- ![GitHub](https://img.shields.io/github/license/shengchenyang/AyugeSpiderTools)
12
- ![python](https://img.shields.io/badge/python-3.8.1%2B-blue)
13
- ![GitHub Workflow Status (with branch)](https://img.shields.io/github/actions/workflow/status/shengchenyang/AyugeSpiderTools/codeql.yml?branch=main)
14
- ![Read the Docs](https://img.shields.io/readthedocs/ayugespidertools)
15
- ![GitHub all releases](https://img.shields.io/github/downloads/shengchenyang/AyugeSpiderTools/total?label=releases%20downloads)
16
- ![PyPI - Downloads](https://img.shields.io/pypi/dm/AyugeSpiderTools?label=pypi%20downloads)
17
-
18
- **简体中文** | [English](./README_en.md)
19
-
20
- ## 概述
21
-
22
- > 一句话介绍:用于扩展 Scrapy 功能来解放双手。
23
-
24
- 在使用 `Scrapy` 开发爬虫时,免不了会重复地编写 `settings`,`items`,`middlewares`,`pipelines`
25
- 和一些通用方法,但各项目中的这些内容都大致相同,那为何不把它们统一整理在一起呢?我也想扩展一些功能,比如当 `spider`
26
- 中添加字段后,不用再修改对应的 `item` 和 `pipeline` 甚至不用手动修改 `Mysql` 表结构,此时使用 `scrapy` 的模板功能就不够了。
27
-
28
- 项目的主旨是让开发者只需专注于 `spider` 脚本的编写,减少开发和维护流程。理想状态下,只需关注 `spider`
29
- 中字段的解析规则和 `VIT` 下的 `.conf` 配置即可,**脱离无意义的重复操作**。
30
-
31
- 以 `Mysql` 存储场景举例:可以自动创建相关数据库,数据表,字段注释,自动添加 `spider`
32
- 中新添加的字段,和自动修复常见(字段编码,`Data too long`,存储字段不存在等)的存储问题。
33
-
34
- ## 安装
35
-
36
- > `python 3.8.1+` 可以直接输入以下命令:
37
-
38
- ```shell
39
- pip install ayugespidertools -i https://pypi.org/simple
40
- ```
41
-
42
- ## 用法
43
-
44
- > 开发人员只需根据命令生成示例模板,再配置相关设置即可。
45
-
46
- 使用方法示例 `GIF` 如下:
47
-
48
- ![ayugespidertools.gif](./examples/ayugespidertools-use.gif)
49
-
50
- 对以上 `GIF` 中的步骤进行解释:
51
-
52
- ```shell
53
- # 查看库版本
54
- ayuge version
55
-
56
- # 创建项目
57
- ayuge startproject <project_name>
58
-
59
- # 进入项目根目录
60
- cd <project_name>
61
-
62
- # 替换(覆盖)为真实的配置 .conf 文件:
63
- # 这里是为了演示方便,正常情况是直接在 VIT 路径下的 .conf 配置文件填上你需要的配置即可
64
- # 不需要的配置强烈建议删除,保证配置的可维护性
65
- cp /root/mytemp/.conf DemoSpider/VIT/.conf
66
-
67
- # 生成爬虫脚本
68
- ayuge genspider <spider_name> <example.com>
69
-
70
- # 运行脚本
71
- scrapy crawl <spider_name>
72
- # 注:也可以使用 ayuge crawl <spider_name>
73
- ```
74
-
75
- 具体的场景案例请在 [DemoSpider](https://github.com/shengchenyang/DemoSpider)
76
- 项目中查看,也可以在 [readthedocs](https://ayugespidertools.readthedocs.io/en/latest/) 文档中查看教程。 目前已适配以下场景:
77
-
78
- ```diff
79
- # 数据存入 Mysql 的场景:
80
- + 1).demo_one: 根据本地 VIT 中的 .conf 取 mysql 配置
81
- + 3).demo_three: 根据 consul 中取 mysql 配置
82
- + 21).demo_mysql_nacos: 根据 nacos 中取 mysql 配置(其它从 nacos 中获取配置的场景不再举例)
83
- + 5).demo_five: 异步存入 Mysql 的场景
84
-
85
- # 数据存入 MongoDB 的场景:
86
- + 2).demo_two: 根据本地 VIT 中的 .conf 取 mongodb 配置
87
- + 4).demo_four: 根据 consul 中取 mongodb 配置
88
- + 6).demo_six: 异步存入 MongoDB 的场景
89
-
90
- - 7).demo_seven: 使用 requests 来请求的场景(已删除此功能,更推荐使用 aiohttp 方式)
91
- + 8).demo_eight: 同时存入 Mysql 和 MongoDB 的场景
92
- + 9).demo_aiohttp_example: 使用 aiohttp 来请求的场景
93
- + 10).demo_aiohttp_test: scrapy aiohttp 在具体项目中的使用方法示例
94
-
95
- + 11).demo_proxy_one: 快代理动态隧道代理示例
96
- + 12).demo_proxy_two: 测试快代理独享代理
97
- +13).demo_AyuTurboMysqlPipeline: mysql 同步连接池的示例
98
- +14).demo_crawl: 支持 scrapy CrawlSpider 的示例
99
-
100
- # 本库中给出支持 Item Loaders 特性的示例(文档地址:https://ayugespidertools.readthedocs.io/en/latest/topics/loaders.html)
101
- +15).demo_item_loader: 本库中使用 Item Loaders 的示例
102
- -16).demo_item_loader_two: 此示例已删除,可查看上个 demo_item_loader 中的示例,目前已经可以很方便的使用 Item Loaders 功能了
103
-
104
- +17).demo_mongo_async: asyncio 版本存储 mongoDB 的 pipelines 示例
105
- +18).demo_mq: 数据存入 rabbitmq 的模板示例
106
- +19).demo_kafka: 数据存入 kafka 的模板示例
107
- +20).demo_file: 下载图片等文件到本地的模板示例
108
- ```
109
-
110
- ## 跑通测试
111
-
112
- 前提:需要在 `tests` 的 `VIT` 目录下创建 `.conf` 文件,已给出示例文件,请填写测试所需内容,然后:
113
-
114
- - 可以直接使用 `tox` 来运行测试。
115
-
116
- - 本库以 [poetry](https://python-poetry.org/docs/) 开发,那么直接新环境下运行 `poetry install`
117
- 后,手动运行目标测试或 `pytest` 命令来测试等皆可。
118
- - 也可以使用 `make` 工具,`make start` 然后 `make test` 即可。
119
-
120
- ## 你可能在意的事
121
-
122
- 1. 若你觉得某些场景下的功能实现不太符合你的预期,想要修改或添加自定义功能,比如移除对你无用模块、修改库名等,你可以自行修改后 `build`。
123
- 2. 本库主推 `scrapy` 扩展(即增强版的自定义模板)的功能,在使用本库时,理论上并不会影响你 `scrapy`
124
- 项目及其它组件。
125
- 3. 如果你想对此项目做出贡献,请参考[pull request 示例](https://ayugespidertools.readthedocs.io/en/latest/additional/contribute.html)。
126
-
127
- ## 构建你的专属库
128
-
129
- > 具体内容请以 [poetry 官方文档](https://python-poetry.org/docs/) 为准。
130
-
131
- 据[你可能在意的事](#你可能在意的事)可知,你可以 `clone`
132
- 源码后,修改任意方法(比如你的项目场景下可能需要其它的日志配置默认值,或添加其它的项目结构模板等),修改完成后 `poetry build`
133
- 或 `make build` 即可打包使用。
134
-
135
- 比如你可能需要更新依赖库中 `pymongo` 为新版本 `x.x.x`,那只需 `poetry install`
136
- 安装现有依赖后,再 `poetry add pymongo@x.x.x` 安装目标版本(尽量不要使用 `poetry update pymongo`
137
- ),确定测试正常了即可 `poetry build` 打包使用。
138
-
139
- **希望此项目能在你遇到扩展 scrapy 功能的场景时能对你有所指引。**
140
-
141
- ## 功能
142
-
143
- - [x] `scrapy` 的扩展功能场景
144
- - [x] `scrapy` 脚本运行信息统计和项目依赖表采集量统计,可用于日志记录和预警
145
- - [x] 自定义模板,在 `ayuge startproject <projname>` 和 `ayuge genspider <spidername>` 时生成适合本库的模板文件
146
- - [x] 从远程应用管理服务中获取项目配置
147
- - [x] 从 `consul` 获取项目配置
148
- - [x] 从 `nacos` 获取项目配置(注意:优先级小于 `consul`)
149
- - [x] 代理中间件(独享代理、动态隧道代理)
150
- - [x] 随机请求头 `UA` 中间件,根据 `fake_useragent` 中的权重来随机
151
- - [x] 使用以下工具来替换 `scrapy` 的 `Request` 来发送请求
152
- - [x] ~~`requests`: 这个不推荐使用,`requests` 同步库会降低 `scrapy` 运行效率~~(已移除此功能,更推荐 `aiohttp`
153
- 的方式)
154
- - [x] `aiohttp`: 集成将 `scrapy Request` 替换为 `aiohttp` 的协程方式
155
- - [x] `Mysql` 存储的场景下适配
156
- - [x] 自动创建 `Mysql` 用户场景下需要的数据库和数据表及字段格式,还有字段注释
157
- - [x] `MongoDB` 存储的场景下适配,编写风格与 `Mysql` 存储等场景下一致
158
- - [x] `asyncio` 语法支持与 `async` 第三方库支持示例
159
- - [x] `spider` 中使用 `asyncio` 的 `aiohttp` 示例
160
- - [x] `pipeline` 中使用 `asyncio` 的 `aioMysql` 示例
161
- - [x] 集成 `Kafka`,`RabbitMQ` 等数据推送功能
162
- - [x] 常用开发场景
163
- - [x] `sql` 语句拼接,只是简单场景,后续优化。已给出优化方向,参考库等信息。
164
- - [x] `mongoDB` 语句拼接
165
- - [x] 数据格式化处理,比如:去除网页标签,去除无效空格等
166
- - [x] 字体反爬还原方法
167
- - [x] 基于 `ttf`,`woff` 之类的字体文件映射,或结合 `css` 等实现
168
- - [x] 可以直接在字体文件 `xml`
169
- 中找到映射关系的:使用 [fontforge](https://github.com/fontforge/fontforge/releases) 工具导出映射即可。
170
- - [x] 无法找到映射关系的,则一般使用 `ocr` 识别(准确率非百分百),通过 `fontforge` 导出每个映射的 `png`
171
- ,后再通过各种方式识别。
172
- - [x] `html` 数据处理,去除标签,不可见字符,特殊字符改成正常显示等
173
- - [x] 添加常用的图片验证码中的处理方法
174
- - [x] 滑块缺口距离的识别方法(多种实现方式)
175
- - [x] 根据滑块距离生成轨迹数组的方法
176
- - [x] 识别点选验证码位置及点击顺序,识别结果不太好,待优化
177
- - [x] 图片乱序混淆的还原方法示例
178
-
179
- 注意:功能演示我将放入 [readthedocs](https://ayugespidertools.readthedocs.io/en/latest/) 文档中,以防此部分内容过多。
1
+ <p align="center">
2
+ <a href="https://ayugespidertools.readthedocs.io" target="_blank" rel="noopener noreferrer">
3
+ <img src="./artwork/ayugespidertools-logo.png" alt="ayugespidertools-logo">
4
+ </a>
5
+ </p>
6
+ <hr/>
7
+
8
+ # AyugeSpiderTools 介绍
9
+
10
+ [![OSCS Status](https://www.oscs1024.com/platform/badge/AyugeSpiderTools.svg?size=small)](https://www.murphysec.com/accept?code=0ec375759aebea7fd260248910b98806&type=1&from=2)
11
+ ![GitHub](https://img.shields.io/github/license/shengchenyang/AyugeSpiderTools)
12
+ ![python](https://img.shields.io/badge/python-3.8.1%2B-blue)
13
+ ![GitHub Workflow Status (with branch)](https://img.shields.io/github/actions/workflow/status/shengchenyang/AyugeSpiderTools/codeql.yml?branch=main)
14
+ ![Read the Docs](https://img.shields.io/readthedocs/ayugespidertools)
15
+ ![GitHub all releases](https://img.shields.io/github/downloads/shengchenyang/AyugeSpiderTools/total?label=releases%20downloads)
16
+ ![PyPI - Downloads](https://img.shields.io/pypi/dm/AyugeSpiderTools?label=pypi%20downloads)
17
+
18
+ **简体中文** | [English](./README_en.md)
19
+
20
+ ## 概述
21
+
22
+ > 一句话介绍:用于扩展 Scrapy 功能来解放双手。
23
+
24
+ 在使用 `Scrapy` 开发爬虫时,免不了会重复地编写 `settings`,`items`,`middlewares`,`pipelines`
25
+ 和一些通用方法,但各项目中的这些内容都大致相同,那为何不把它们统一整理在一起呢?我也想扩展一些功能,比如当 `spider`
26
+ 中添加字段后,不用再修改对应的 `item` 和 `pipeline` 甚至不用手动修改 `Mysql` 表结构,此时使用 `scrapy` 的模板功能就不够了。
27
+
28
+ 项目的主旨是让开发者只需专注于 `spider` 脚本的编写,减少开发和维护流程。理想状态下,只需关注 `spider`
29
+ 中字段的解析规则和 `VIT` 下的 `.conf` 配置即可,**脱离无意义的重复操作**。
30
+
31
+ 以 `Mysql` 存储场景举例:可以自动创建相关数据库,数据表,字段注释,自动添加 `spider`
32
+ 中新添加的字段,和自动修复常见(字段编码,`Data too long`,存储字段不存在等)的存储问题。
33
+
34
+ ## 安装
35
+
36
+ > `python 3.8.1+` 可以直接输入以下命令:
37
+
38
+ ```shell
39
+ pip install ayugespidertools -i https://pypi.org/simple
40
+ ```
41
+
42
+ ## 用法
43
+
44
+ > 开发人员只需根据命令生成示例模板,再配置相关设置即可。
45
+
46
+ 使用方法示例 `GIF` 如下:
47
+
48
+ ![ayugespidertools.gif](./examples/ayugespidertools-use.gif)
49
+
50
+ 对以上 `GIF` 中的步骤进行解释:
51
+
52
+ ```shell
53
+ # 查看库版本
54
+ ayuge version
55
+
56
+ # 创建项目
57
+ ayuge startproject <project_name>
58
+
59
+ # 进入项目根目录
60
+ cd <project_name>
61
+
62
+ # 替换(覆盖)为真实的配置 .conf 文件:
63
+ # 这里是为了演示方便,正常情况是直接在 VIT 路径下的 .conf 配置文件填上你需要的配置即可
64
+ # 不需要的配置强烈建议删除,保证配置的可维护性
65
+ cp /root/mytemp/.conf DemoSpider/VIT/.conf
66
+
67
+ # 生成爬虫脚本
68
+ ayuge genspider <spider_name> <example.com>
69
+
70
+ # 运行脚本
71
+ scrapy crawl <spider_name>
72
+ # 注:也可以使用 ayuge crawl <spider_name>
73
+ ```
74
+
75
+ 具体的场景案例请在 [DemoSpider](https://github.com/shengchenyang/DemoSpider)
76
+ 项目中查看,也可以在 [readthedocs](https://ayugespidertools.readthedocs.io/en/latest/) 文档中查看教程。 目前已适配以下场景:
77
+
78
+ ```diff
79
+ # 数据存入 Mysql 的场景:
80
+ + 1).demo_one: 根据本地 VIT 中的 .conf 取 mysql 配置
81
+ + 3).demo_three: 根据 consul 中取 mysql 配置
82
+ + 21).demo_mysql_nacos: 根据 nacos 中取 mysql 配置(其它从 nacos 中获取配置的场景不再举例)
83
+ + 5).demo_five: 异步存入 Mysql 的场景
84
+
85
+ # 数据存入 MongoDB 的场景:
86
+ + 2).demo_two: 根据本地 VIT 中的 .conf 取 mongodb 配置
87
+ + 4).demo_four: 根据 consul 中取 mongodb 配置
88
+ + 6).demo_six: 异步存入 MongoDB 的场景
89
+
90
+ - 7).demo_seven: 使用 requests 来请求的场景(已删除此功能,更推荐使用 aiohttp 方式)
91
+ + 8).demo_eight: 同时存入 Mysql 和 MongoDB 的场景
92
+ + 9).demo_aiohttp_example: 使用 aiohttp 来请求的场景
93
+ + 10).demo_aiohttp_test: scrapy aiohttp 在具体项目中的使用方法示例
94
+
95
+ + 11).demo_proxy_one: 快代理动态隧道代理示例
96
+ + 12).demo_proxy_two: 测试快代理独享代理
97
+ +13).demo_AyuTurboMysqlPipeline: mysql 同步连接池的示例
98
+ +14).demo_crawl: 支持 scrapy CrawlSpider 的示例
99
+
100
+ # 本库中给出支持 Item Loaders 特性的示例(文档地址:https://ayugespidertools.readthedocs.io/en/latest/topics/loaders.html)
101
+ +15).demo_item_loader: 本库中使用 Item Loaders 的示例
102
+ -16).demo_item_loader_two: 此示例已删除,可查看上个 demo_item_loader 中的示例,目前已经可以很方便的使用 Item Loaders 功能了
103
+
104
+ +17).demo_mongo_async: asyncio 版本存储 mongoDB 的 pipelines 示例
105
+ +18).demo_mq: 数据存入 rabbitmq 的模板示例
106
+ +19).demo_kafka: 数据存入 kafka 的模板示例
107
+ +20).demo_file: 下载图片等文件到本地的模板示例
108
+ ```
109
+
110
+ ## 跑通测试
111
+
112
+ 前提:需要在 `tests` 的 `VIT` 目录下创建 `.conf` 文件,已给出示例文件,请填写测试所需内容,然后:
113
+
114
+ - 可以直接使用 `tox` 来运行测试。
115
+
116
+ - 本库以 [poetry](https://python-poetry.org/docs/) 开发,那么直接新环境下运行 `poetry install`
117
+ 后,手动运行目标测试或 `pytest` 命令来测试等皆可。
118
+ - 也可以使用 `make` 工具,`make start` 然后 `make test` 即可。
119
+
120
+ ## 你可能在意的事
121
+
122
+ 1. 若你觉得某些场景下的功能实现不太符合你的预期,想要修改或添加自定义功能,比如移除对你无用模块、修改库名等,你可以自行修改后 `build`。
123
+ 2. 本库主推 `scrapy` 扩展(即增强版的自定义模板)的功能,在使用本库时,理论上并不会影响你 `scrapy`
124
+ 项目及其它组件。
125
+ 3. 如果你想对此项目做出贡献,请参考[pull request 示例](https://ayugespidertools.readthedocs.io/en/latest/additional/contribute.html)。
126
+
127
+ ## 构建你的专属库
128
+
129
+ > 具体内容请以 [poetry 官方文档](https://python-poetry.org/docs/) 为准。
130
+
131
+ 据[你可能在意的事](#你可能在意的事)可知,你可以 `clone`
132
+ 源码后,修改任意方法(比如你的项目场景下可能需要其它的日志配置默认值,或添加其它的项目结构模板等),修改完成后 `poetry build`
133
+ 或 `make build` 即可打包使用。
134
+
135
+ 比如你可能需要更新依赖库中 `pymongo` 为新版本 `x.x.x`,那只需 `poetry install`
136
+ 安装现有依赖后,再 `poetry add pymongo@x.x.x` 安装目标版本(尽量不要使用 `poetry update pymongo`
137
+ ),确定测试正常了即可 `poetry build` 打包使用。
138
+
139
+ **希望此项目能在你遇到扩展 scrapy 功能的场景时能对你有所指引。**
140
+
141
+ ## 功能
142
+
143
+ - [x] `scrapy` 的扩展功能场景
144
+ - [x] `scrapy` 脚本运行信息统计和项目依赖表采集量统计,可用于日志记录和预警
145
+ - [x] 自定义模板,在 `ayuge startproject <projname>` 和 `ayuge genspider <spidername>` 时生成适合本库的模板文件
146
+ - [x] 从远程应用管理服务中获取项目配置
147
+ - [x] 从 `consul` 获取项目配置
148
+ - [x] 从 `nacos` 获取项目配置(注意:优先级小于 `consul`)
149
+ - [x] 代理中间件(独享代理、动态隧道代理)
150
+ - [x] 随机请求头 `UA` 中间件,根据 `fake_useragent` 中的权重来随机
151
+ - [x] 使用以下工具来替换 `scrapy` 的 `Request` 来发送请求
152
+ - [x] ~~`requests`: 这个不推荐使用,`requests` 同步库会降低 `scrapy` 运行效率~~(已移除此功能,更推荐 `aiohttp`
153
+ 的方式)
154
+ - [x] `aiohttp`: 集成将 `scrapy Request` 替换为 `aiohttp` 的协程方式
155
+ - [x] `Mysql` 存储的场景下适配
156
+ - [x] 自动创建 `Mysql` 用户场景下需要的数据库和数据表及字段格式,还有字段注释
157
+ - [x] `MongoDB` 存储的场景下适配,编写风格与 `Mysql` 存储等场景下一致
158
+ - [x] `asyncio` 语法支持与 `async` 第三方库支持示例
159
+ - [x] `spider` 中使用 `asyncio` 的 `aiohttp` 示例
160
+ - [x] `pipeline` 中使用 `asyncio` 的 `aioMysql` 示例
161
+ - [x] 集成 `Kafka`,`RabbitMQ` 等数据推送功能
162
+ - [x] 常用开发场景
163
+ - [x] `sql` 语句拼接,只是简单场景,后续优化。已给出优化方向,参考库等信息。
164
+ - [x] `mongoDB` 语句拼接
165
+ - [x] 数据格式化处理,比如:去除网页标签,去除无效空格等
166
+ - [x] 字体反爬还原方法
167
+ - [x] 基于 `ttf`,`woff` 之类的字体文件映射,或结合 `css` 等实现
168
+ - [x] 可以直接在字体文件 `xml`
169
+ 中找到映射关系的:使用 [fontforge](https://github.com/fontforge/fontforge/releases) 工具导出映射即可。
170
+ - [x] 无法找到映射关系的,则一般使用 `ocr` 识别(准确率非百分百),通过 `fontforge` 导出每个映射的 `png`
171
+ ,后再通过各种方式识别。
172
+ - [x] `html` 数据处理,去除标签,不可见字符,特殊字符改成正常显示等
173
+ - [x] 添加常用的图片验证码中的处理方法
174
+ - [x] 滑块缺口距离的识别方法(多种实现方式)
175
+ - [x] 根据滑块距离生成轨迹数组的方法
176
+ - [x] 识别点选验证码位置及点击顺序,识别结果不太好,待优化
177
+ - [x] 图片乱序混淆的还原方法示例
178
+
179
+ 注意:功能演示我将放入 [readthedocs](https://ayugespidertools.readthedocs.io/en/latest/) 文档中,以防此部分内容过多。
@@ -1,5 +1,5 @@
1
1
  from abc import ABC, abstractmethod
2
- from typing import TYPE_CHECKING, TypeVar, Union
2
+ from typing import TYPE_CHECKING, Tuple, TypeVar, Union
3
3
 
4
4
  from ayugespidertools.common.multiplexing import ReuseOperation
5
5
 
@@ -23,7 +23,7 @@ class AbstractClass(ABC):
23
23
  def _get_insert_data(
24
24
  self,
25
25
  item_dict: Union["ItemAdapter", dict],
26
- ) -> dict:
26
+ ) -> Tuple[dict, str]:
27
27
  """获取要插入的数据,将 item 中的存储数据提取出来
28
28
 
29
29
  Args:
@@ -34,14 +34,16 @@ class AbstractClass(ABC):
34
34
  """
35
35
  insert_data = ReuseOperation.get_items_except_keys(
36
36
  dict_conf=item_dict,
37
- keys=["_table", "_item_mode", "_mongo_update_rule"],
37
+ keys=["_table", "_mongo_update_rule"],
38
38
  )
39
+ table_name = item_dict["_table"]
39
40
  judge_item = next(iter(insert_data.values()))
40
41
  # 是 namedtuple 类型
41
42
  if ReuseOperation.is_namedtuple_instance(judge_item):
42
43
  insert_data = {v: insert_data[v].key_value for v in insert_data.keys()}
44
+ table_name = item_dict["_table"].key_value
43
45
  # 是普通的 dict 格式,则直接为 insert_data
44
- return insert_data
46
+ return insert_data, table_name
45
47
 
46
48
  def process_item_template(
47
49
  self,
@@ -56,11 +58,11 @@ class AbstractClass(ABC):
56
58
  db: mongodb 数据库连接
57
59
  sys_ver_low: 是否是 py3.11 以下
58
60
  """
59
- insert_data = self._get_insert_data(item_dict)
61
+ insert_data, table_name = self._get_insert_data(item_dict)
60
62
  self._data_storage_logic(
61
63
  db=db,
62
64
  item_dict=item_dict,
63
- collection_name=item_dict["_table"],
65
+ collection_name=table_name,
64
66
  insert_data=insert_data,
65
67
  sys_ver_low=sys_ver_low,
66
68
  )
@@ -170,11 +172,11 @@ class AsyncioAsynchronous(AbstractClass):
170
172
  db: "PymongoDataBase",
171
173
  sys_ver_low: bool = True,
172
174
  ) -> None:
173
- insert_data = self._get_insert_data(item_dict)
175
+ insert_data, table_name = self._get_insert_data(item_dict)
174
176
  await self._data_storage_logic(
175
177
  db=db,
176
178
  item_dict=item_dict,
177
- collection_name=item_dict["_table"],
179
+ collection_name=table_name,
178
180
  insert_data=insert_data,
179
181
  )
180
182
 
@@ -17,6 +17,8 @@ __all__ = [
17
17
  ]
18
18
 
19
19
  if TYPE_CHECKING:
20
+ from pathlib import Path
21
+
20
22
  from scrapy.settings import BaseSettings
21
23
 
22
24
  from ayugespidertools.common.typevars import MysqlConf
@@ -26,14 +28,12 @@ class ReuseOperation:
26
28
  """用于存放经常复用的一些操作"""
27
29
 
28
30
  @staticmethod
29
- def fetch_local_conf(
30
- vit_dir: str, inner_settings: "BaseSettings"
31
- ) -> "BaseSettings":
31
+ def fetch_local_conf(vit_dir: Union[str, "Path"], inner_settings: dict) -> dict:
32
32
  """通过本地 VIT 中的 .conf 获取所需配置,并将其添加到 inner_settings
33
33
 
34
34
  Args:
35
35
  vit_dir: 配置文件所在的目录
36
- inner_settings: scrapy 的 inner_settings
36
+ inner_settings: inner_settings 配置
37
37
 
38
38
  Returns:
39
39
  inner_settings: 本库所需的配置
@@ -1,8 +1,7 @@
1
1
  import re
2
2
  from abc import ABC, abstractmethod
3
- from typing import TYPE_CHECKING, Optional, Tuple, Type, TypeVar, Union
3
+ from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Union
4
4
 
5
- from ayugespidertools.common.typevars import TableEnumTypeVar
6
5
  from ayugespidertools.config import logger
7
6
 
8
7
  __all__ = [
@@ -30,7 +29,6 @@ class AbstractClass(ABC):
30
29
  charset: str,
31
30
  collate: str,
32
31
  table_notes: str = "",
33
- demand_code: str = "",
34
32
  ) -> None:
35
33
  """创建数据库表
36
34
 
@@ -44,12 +42,7 @@ class AbstractClass(ABC):
44
42
  charset: charset
45
43
  collate: collate
46
44
  table_notes: 创建表的注释
47
- demand_code: 创建表的需求对应的 code 值,用于和需求中的任务对应
48
45
  """
49
- # 用于表格 comment 的参数生成(即 table_notes 参数)
50
- if demand_code != "":
51
- table_notes = f"{demand_code}_{table_notes}"
52
-
53
46
  sql = f"""
54
47
  CREATE TABLE IF NOT EXISTS `{table_name}`
55
48
  (`id` int(32) NOT NULL AUTO_INCREMENT COMMENT 'id', PRIMARY KEY (`id`))
@@ -113,7 +106,7 @@ class AbstractClass(ABC):
113
106
  collate: str,
114
107
  database: str,
115
108
  table: str,
116
- table_enum: Type[TableEnumTypeVar],
109
+ table_notes: str,
117
110
  note_dic: dict,
118
111
  ) -> None:
119
112
  """模板方法,用于处理 mysql 存储场景的异常
@@ -126,7 +119,7 @@ class AbstractClass(ABC):
126
119
  collate: mysql table collate
127
120
  database: 数据库
128
121
  table: 数据表
129
- table_enum: 数据表枚举类
122
+ table_notes: 数据表注释
130
123
  note_dic: 当前表字段注释
131
124
  """
132
125
  if "1054" in err_msg:
@@ -136,17 +129,12 @@ class AbstractClass(ABC):
136
129
  self._exec_sql(conn=conn, cursor=cursor, sql=sql, possible_err=possible_err)
137
130
 
138
131
  elif "1146" in err_msg:
139
- table_name, table_notes, demand_code = self.deal_1146_error(
140
- err_msg=err_msg,
141
- table_enum=table_enum,
142
- )
143
132
  self._create_table(
144
133
  cursor=cursor,
145
- table_name=table_name,
134
+ table_name=table,
146
135
  charset=charset,
147
136
  collate=collate,
148
137
  table_notes=table_notes,
149
- demand_code=demand_code,
150
138
  )
151
139
 
152
140
  elif "1406" in err_msg:
@@ -195,40 +183,6 @@ class AbstractClass(ABC):
195
183
  sql = f"ALTER TABLE `{table}` ADD COLUMN `{colum}` VARCHAR(190) NULL DEFAULT '' COMMENT '{notes}';"
196
184
  return sql, f"1054: 添加字段 {colum} 已存在"
197
185
 
198
- def deal_1146_error(
199
- self,
200
- err_msg: str,
201
- table_enum: Type[TableEnumTypeVar],
202
- ) -> Tuple[str, str, str]:
203
- """解决 1146, u"Table 'xx' doesn't exist"
204
-
205
- Args:
206
- err_msg: 报错内容
207
- table_enum: 数据表的枚举信息
208
-
209
- Returns:
210
- 1). table_name: 数据表名
211
- 2). table_notes: 数据表注释
212
- 3). demand_code: 数据表对应的需求 code
213
- """
214
- table_pattern = re.compile(r"Table '(.*?)' doesn't exist")
215
- text = re.findall(table_pattern, err_msg)
216
- table = text[0].split(".")[1]
217
-
218
- # 写入表枚举
219
- if table_enum:
220
- for _, member in table_enum.__members__.items():
221
- table_name = member.value.get("value", "")
222
- table_notes = member.value.get("notes", "")
223
- demand_code = member.value.get("demand_code", "")
224
- if table_name == table:
225
- return table_name, table_notes, demand_code
226
- else:
227
- # 未定义 Tabel_Enum 则建表
228
- logger.info("未定义数据库表枚举 Tabel_Enum 参数,进行创表操作")
229
- # 1.未定义 Tabel_Enum 和 2.正则未匹配到当前的 table_name 时都以此报错 table 名为准
230
- return table, "", ""
231
-
232
186
  def deal_1406_error(
233
187
  self,
234
188
  err_msg: str,
@@ -357,7 +311,7 @@ def deal_mysql_err(
357
311
  collate: str,
358
312
  database: str,
359
313
  table: str,
360
- table_enum: Type[TableEnumTypeVar],
314
+ table_notes: str,
361
315
  note_dic: dict,
362
316
  conn: Optional[PymysqlConnect] = None,
363
317
  ) -> None:
@@ -369,6 +323,6 @@ def deal_mysql_err(
369
323
  collate,
370
324
  database,
371
325
  table,
372
- table_enum,
326
+ table_notes,
373
327
  note_dic,
374
328
  )
@@ -1,16 +1,6 @@
1
1
  # Define your TypeVar here
2
2
  from dataclasses import dataclass, field
3
- from enum import Enum, unique
4
- from typing import (
5
- TYPE_CHECKING,
6
- List,
7
- Literal,
8
- NamedTuple,
9
- Optional,
10
- TypedDict,
11
- TypeVar,
12
- Union,
13
- )
3
+ from typing import TYPE_CHECKING, List, Literal, NamedTuple, Optional, TypeVar, Union
14
4
 
15
5
  if TYPE_CHECKING:
16
6
  from itemadapter import ItemAdapter
@@ -22,30 +12,9 @@ I_Str_N = TypeVar("I_Str_N", int, str, NoneType)
22
12
  Str_Lstr = TypeVar("Str_Lstr", str, List[str])
23
13
 
24
14
  AiohttpRequestMethodStr = Literal["GET", "POST"]
25
- TableEnumTypeVar = TypeVar("TableEnumTypeVar", bound="TableEnum")
26
15
  ItemAdapterType = TypeVar("ItemAdapterType", bound="ItemAdapter")
27
16
 
28
17
 
29
- class TableTemplate(TypedDict):
30
- # 存储表名
31
- value: str
32
- # 表注释信息
33
- notes: str
34
- # 需求表对应数据,可用于开发任务与需求任务表中任务 code 对应
35
- demand_code: str
36
-
37
-
38
- @unique
39
- class TableEnum(Enum):
40
- """数据库表枚举信息示例,用于限制存储信息类的字段及值不允许重复和修改"""
41
-
42
- demo_table = TableTemplate(
43
- value="表名(eg: demo)",
44
- notes="表注释信息(eg: 示例表信息)",
45
- demand_code="需求表对应数据(eg: Demo_table_demand_code,此示例没有意义,需要自定义)",
46
- )
47
-
48
-
49
18
  class MysqlConf(NamedTuple):
50
19
  host: str
51
20
  port: int