【发布时间】:2016-11-18 09:17:16
【问题描述】:
我想将 scrapy 作为 python 脚本运行,但我不知道如何正确设置设置或如何提供它们。我不确定这是否是设置问题,但我认为是。
我的配置:
- Python 2.7 x86(作为虚拟环境)
- Scrapy 1.2.1
- Win 7 x64
我接受了https://doc.scrapy.org/en/latest/topics/practices.html#run-scrapy-from-a-script 的建议,让它运行起来。我对以下建议有一些疑问:
如果您在 Scrapy 项目中,则可以使用一些额外的帮助程序在项目中导入这些组件。您可以自动导入您的蜘蛛并将它们的名称传递给 CrawlerProcess,并使用 get_project_settings 获取带有您项目设置的 Settings 实例。
那么“在 Scrapy 项目中”是什么意思?当然我必须导入库并安装依赖项,但我想避免使用scrapy crawl xyz 开始爬取过程。
这是 myScrapy.py 的代码
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.item import Item, Field
import os, argparse
#Initialization of directories
projectDir = os.path.dirname(os.path.realpath('__file__'))
generalOutputDir = os.path.join(projectDir, 'output')
parser = argparse.ArgumentParser()
parser.add_argument("url", help="The url which you want to scan", type=str)
args = parser.parse_args()
urlToScan = args.url
#Stripping of given URL to get only the host + TLD
if "https" in urlToScan:
urlToScanNoProt = urlToScan.replace("https://","")
print "used protocol: https"
if "http" in urlToScan:
urlToScanNoProt = urlToScan.replace("http://","")
print "used protocol: http"
class myItem(Item):
url = Field()
class mySpider(CrawlSpider):
name = "linkspider"
allowed_domains = [urlToScanNoProt]
start_urls = [urlToScan,]
rules = (Rule(LinkExtractor(), callback='parse_url', follow=True), )
def generateDirs(self):
if not os.path.exists(generalOutputDir):
os.makedirs(generalOutputDir)
specificOutputDir = os.path.join(generalOutputDir, urlToScanNoProt)
if not os.path.exists(specificOutputDir):
os.makedirs(specificOutputDir)
return specificOutputDir
def parse_url(self, response):
for link in LinkExtractor().extract_links(response):
item = myItem()
item['url'] = response.url
specificOutputDir = self.generateDirs()
filename = os.path.join(specificOutputDir, response.url.split("/")[-2] + ".html")
with open(filename, "wb") as f:
f.write(response.body)
return CrawlSpider.parse(self, response)
return item
process = CrawlerProcess(get_project_settings())
process.crawl(mySpider)
process.start() # the script will block here until the crawling is finished
为什么我必须打电话给process.crawl(mySpider) 而不是process.crawl(linkspider)?我认为获取设置是一个问题,因为它们是在“正常”scrapy-project(你必须在其中运行scrapy crawl xyz)中设置的,因为putput说
2016-11-18 10:38:42 [scrapy] INFO: Overridden settings: {}
我希望你能理解我的问题(英语不是我的母语……;))
提前致谢!
【问题讨论】:
标签: python python-2.7 scrapy scrapy-spider