我有一个利用代理的网络爬虫。我有一个脚本,它生成一个包含100个有效代理的列表,然后在settings.py中将该列表设置为代理源。我的问题是,目前我手动运行一个生成该文件的脚本,然后运行爬虫。
如果我想让代码在settings.py被“处理”之前运行,有人知道我会把它放在哪里吗?我不想在运行爬虫之前手动运行这个脚本,因为我希望它是独立包含的。ROTATING_PROXY_LIST_PATH = 'C:\\Users\\cmdan\\Desktop\\Spiders\\Michael Mitarotonda\\proxies.txt'
提前感谢!
发布于 2021-03-23 00:21:38
Docs向从脚本中运行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 finishedhttps://stackoverflow.com/questions/66755485
复制相似问题