[英]Scrapy Run code before loading settings.py
我有一個使用代理的 web 爬蟲。 我有一個生成 100 個有效代理列表的腳本,然后我將該列表設置為 settings.py 中的代理源。 我的問題是,目前我手動運行生成該文件的腳本,然后運行爬蟲。
如果我希望它在 settings.py 被“處理”之前運行,有誰知道我會把該代碼放在哪里? 我不想在運行爬蟲之前手動運行該腳本,因為我希望它是自包含的。 ROTATING_PROXY_LIST_PATH = 'C:\\Users\\cmdan\\Desktop\\Spiders\\Michael Mitarotonda\\proxies.txt'
提前致謝!
文檔解釋了從腳本運行 Scrapy的方法。 這意味着它應該允許您在運行爬蟲之前執行一些其他操作,例如您的代理腳本。
你可能想在這個腳本中定義你的爬蟲,或者你可能想導入你的爬蟲,兩者都可以。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.