首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载settings.py之前先运行代码

加载settings.py之前先运行代码
EN

Stack Overflow用户
提问于 2021-03-23 00:06:43
回答 1查看 114关注 0票数 0

我有一个利用代理的网络爬虫。我有一个脚本,它生成一个包含100个有效代理的列表,然后在settings.py中将该列表设置为代理源。我的问题是,目前我手动运行一个生成该文件的脚本,然后运行爬虫。

如果我想让代码在settings.py被“处理”之前运行,有人知道我会把它放在哪里吗?我不想在运行爬虫之前手动运行这个脚本,因为我希望它是独立包含的。ROTATING_PROXY_LIST_PATH = 'C:\\Users\\cmdan\\Desktop\\Spiders\\Michael Mitarotonda\\proxies.txt'

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-23 00:21:38

Docs向从脚本中运行Scrapy解释该方法。这意味着它应该允许您在运行爬虫之前执行其他一些操作,比如代理脚本。

您可能希望在此脚本中定义您的爬虫,或者您可能希望导入您的爬虫,两者都可以。

代码语言:javascript
复制
import scrapy
from scrapy.crawler import CrawlerProcess

# if you want to import your spider
# from project.spiders import myspider

class MySpider(scrapy.Spider):
    # Your spider definition
    ...

# here comes your script, setting the value of
# ROTATING_PROXY_LIST_PATH

process = CrawlerProcess(settings={
    "FEEDS": {
        "items.json": {"format": "json"},
    },
    "ROTATING_PROXY_LIST_PATH": "path-to-file",
})

process.crawl(MySpider)
process.start() # the script will block here until the crawling is finished
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66755485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档