通常掸冤,運(yùn)行scrapy爬蟲(chóng)的方式是在命令行輸入scrapy crawl <spider_name>
,調(diào)試的常用方式是在命令行輸入scrapy shell <url_name>
∈成耄總的來(lái)說(shuō)流炕,調(diào)試方法比較單一澎现。其實(shí),還有兩種調(diào)試方法每辟,可以在pycharm中實(shí)現(xiàn)調(diào)試剑辫。
1.使用scrapy.cmdline的execute方法
首先,在項(xiàng)目文件scrapy.cfg的同級(jí)建立main.py文件(注意渠欺,必須是同級(jí)建立)妹蔽,在其中鍵入如下代碼:
from scrapy.cmdline import execute
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
execute(['scrapy', 'crawl', 'spider_name']) # 你需要將此處的spider_name替換為你自己的爬蟲(chóng)名稱
在其余爬蟲(chóng)文件中設(shè)置斷點(diǎn)后,運(yùn)行(debug)main.py,即可實(shí)現(xiàn)在pycharm中的調(diào)試挠将。
2.使用scrapy的CrawlerProcess方法
在項(xiàng)目文件scrapy.cfg的同級(jí)建立main.py文件(注意胳岂,必須是同級(jí)建立),在其中鍵入如下代碼:
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
if __name__ == '__main__':
process = CrawlerProcess(get_project_settings())
process.crawl('spider_name') # 你需要將此處的spider_name替換為你自己的爬蟲(chóng)名稱
process.start()
在其余爬蟲(chóng)文件中設(shè)置斷點(diǎn)后舔稀,運(yùn)行(debug)main.py,即可實(shí)現(xiàn)在pycharm中的調(diào)試乳丰。
兩種方式都很簡(jiǎn)單實(shí)用,值得掌握内贮。