【发布时间】:2022-01-17 04:44:30
【问题描述】:
我有以下工作蜘蛛作为一个进程 - 所以我可以使用以下命令启动蜘蛛: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 = False,AUTOTHROTTLE_ENABLED = True,AUTOTHROTTLE_START_DELAY = 5 等等。
我怎样才能以这种方式输出到 json - 所以输出会被覆盖? (当我现在运行程序时,数据总是附加到现有的 items.json 文件中) (当我用 scrapy runpider 运行蜘蛛时,我会使用 -o 或 -O 来覆盖或不覆盖输出文件 - 但我该怎么做呢?)
【问题讨论】:
标签: python web-scraping scrapy