一个简单的配件工具刮。
scrapy-accessor的Python项目详细描述
破烂配件
简介
Scrapy的有用辅助工具。在
包含:
- 中间件
- 饲料导出器存储后端
安装
pip install scrapy-accessory
使用
中间件
RandomUserAgentDownloadMiddleware
向请求添加随机用户代理。在
在设置.py添加
^{pr2}$您可以使用USER_AGENT_LIST_FILE
或USER_AGENT_LIST
来配置用户代理。
USER_AGENT_LIST_FILE
指向每行包含一个用户代理的文本文件。
USER_AGENT_LIST
是用户代理的列表或元组。在
代理下载中间件
为请求添加http或https代理。在
在设置.py添加
PROXY_ENABLED = True # True to use proxy, default is False
# PROXY_HOST = 'localhost:8080' # default static proxy, format: <ip>:<port>, default empty
PROXY_CACHE = 'redis://localhost:6379/0' # cache for proxy, use redis://<host>:<port>/<db> to use redis cache, default dict in memory
PROXY_TTL = 30 # proxy cache ttl in seconds, default 30s
CHANGE_PROXY_STATUS = [429] # a list of status codes that force to change proxy if received, default [429]
默认值是在中配置的静态代理设置.py,您可以从API或其他方式添加动态代理。
只需要扩展ProxyDownloadMiddleware
类并实现generate_proxy
方法。在
示例:
class DynamicProxyDownloadMiddleware(ProxyDownloadMiddleware):
api = 'http://api-to-get-proxy-ip'
def generate_proxy(self):
res = requests.get(self.api)
if res.status_code < 300:
return res.text # return format <ip>:<port>
return None
饲料导出器存储后端
ObsFeedStorage
华为云OBS的Feed exporter存储后端。在
先安装obs sdk
pip install esdk-obs-python
在中配置设置.py在
FEED_STORAGES={'obs':'scrapy_accessory.feedexporter.ObsFeedStorage',}HUAWEI_ACCESS_KEY_ID='<your access key id>'HUAWEI_SECRET_ACCESS_KEY='<your secret access key>'HUAWEI_OBS_ENDPOINT='<your obs bucket endpoint> ex: https://obs.cn-north-4.myhuaweicloud.com'
通过OBS模式输出到OBS -o obs://<bucket>/<key>
骨饲料储存
阿里云OSS的Feed exporter存储后端。在
先安装oss sdk
pip install oss2
在中配置设置.py在
FEED_STORAGES={'oss':'scrapy_accessory.feedexporter.OssFeedStorage',}ALI_ACCESS_KEY_ID='<your access key id>'ALI_SECRET_ACCESS_KEY='<your secret access key>'ALI_OSS_ENDPOINT='<your oss bucket endpoint> ex: https://oss-cn-beijing.aliyuncs.com'
通过OSS schema输出到OSS -o oss://<bucket>/<key>
- 项目
标签: