更新时间:2023-11-19 14:11:10
简单明了 :)
只需查看官方文档.我会做一些改变,这样你就可以控制蜘蛛只在你执行 python myscript.py
时运行,而不是每次从它导入时.只需添加一个 if __name__ == "__main__"
:
Just check the official documentation. I would make there a little change so you could control the spider to run only when you do python myscript.py
and not every time you just import from it. Just add an if __name__ == "__main__"
:
import scrapy
from scrapy.crawler import CrawlerProcess
class MySpider(scrapy.Spider):
# Your spider definition
pass
if __name__ == "__main__":
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
process.crawl(MySpider)
process.start() # the script will block here until the crawling is finished
现在将文件另存为 myscript.py
并运行python myscript.py".
Now save the file as myscript.py
and run 'python myscript.py`.
享受吧!