Scrapy runspider / 如何使用不同的设置?
Scrapy runspider / how to use different settings?
我有以下工作蜘蛛作为一个进程 -
所以我可以用以下命令启动蜘蛛:python xyz.py
import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
class onlyLinks(scrapy.Spider):
name = 'onlyLinks'
allowed_domains = ['magnatiles.com']
start_urls = ['https://www.magnatiles.com/products/page/1/']
def parse(self, response):
tmpProd = response.xpath("//ul[@class='products']//ancestor::li")
for p in tmpProd:
yield {
"link": p.xpath("(./a)[1]/@href").get(),
"name": p.xpath(".//h2/text()").get(),
"sku": p.css("a.button::attr(data-product_sku)").get(),
"price": p.xpath("//span[@class='price']//ancestor::bdi/text()").get()
}
process = CrawlerProcess(settings={
"FEEDS": {
"items.json": {"format": "json"},
# "items.csv": {"format": "csv"},
# "items.xlsx": {"format": "xlsx"},
},
})
process.crawl(onlyLinks)
process.start()
但是我如何使用 settings.py 中的其他信息/设置?
例如。 ROBOTSTXT_OBEY = 假,AUTOTHROTTLE_ENABLED = 真,AUTOTHROTTLE_START_DELAY = 5 等等
我怎样才能以这种方式将输出输出到 json - 这样输出就会被覆盖?
(当我 运行 程序现在数据总是附加到现有的 items.json 文件)
(当我 运行 一个带有 scrapy 运行spider 的蜘蛛时,我会使用 -o 或 -O 来覆盖或不覆盖输出文件 - 但我怎么能在这里做到这一点?)
您可以使用键值对来做到这一点 dict
格式如下:
process = CrawlerProcess ({
"FEEDS": {"items.json": {"format": "json", "overwrite": True}},
'ROBOTSTXT_OBEY':'False',
'USER_AGENT': 'Mozilla/5.0',
'AUTOTHROTTLE_ENABLED':'True',
'AUTOTHROTTLE_START_DELAY': '5'
})
process.crawl(onlyLinks)
process.start()
我有以下工作蜘蛛作为一个进程 - 所以我可以用以下命令启动蜘蛛:python xyz.py
import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
class onlyLinks(scrapy.Spider):
name = 'onlyLinks'
allowed_domains = ['magnatiles.com']
start_urls = ['https://www.magnatiles.com/products/page/1/']
def parse(self, response):
tmpProd = response.xpath("//ul[@class='products']//ancestor::li")
for p in tmpProd:
yield {
"link": p.xpath("(./a)[1]/@href").get(),
"name": p.xpath(".//h2/text()").get(),
"sku": p.css("a.button::attr(data-product_sku)").get(),
"price": p.xpath("//span[@class='price']//ancestor::bdi/text()").get()
}
process = CrawlerProcess(settings={
"FEEDS": {
"items.json": {"format": "json"},
# "items.csv": {"format": "csv"},
# "items.xlsx": {"format": "xlsx"},
},
})
process.crawl(onlyLinks)
process.start()
但是我如何使用 settings.py 中的其他信息/设置? 例如。 ROBOTSTXT_OBEY = 假,AUTOTHROTTLE_ENABLED = 真,AUTOTHROTTLE_START_DELAY = 5 等等
我怎样才能以这种方式将输出输出到 json - 这样输出就会被覆盖? (当我 运行 程序现在数据总是附加到现有的 items.json 文件) (当我 运行 一个带有 scrapy 运行spider 的蜘蛛时,我会使用 -o 或 -O 来覆盖或不覆盖输出文件 - 但我怎么能在这里做到这一点?)
您可以使用键值对来做到这一点 dict
格式如下:
process = CrawlerProcess ({
"FEEDS": {"items.json": {"format": "json", "overwrite": True}},
'ROBOTSTXT_OBEY':'False',
'USER_AGENT': 'Mozilla/5.0',
'AUTOTHROTTLE_ENABLED':'True',
'AUTOTHROTTLE_START_DELAY': '5'
})
process.crawl(onlyLinks)
process.start()