我从命令 python 收集 url,然后将它的插入到 start_urlsfrom flask import Flask, jsonify, requestimport scrapyimport subprocessclass ClassSpider(scrapy.Spider): name = 'mySpider' #start_urls = [] #pages = 0 news = [] def __init__(self, url, nbrPage): self.pages = nbrPage self.start_urls = [] self.start_urlsappend(url) def parse(self): ... def run(self): subprocess.check_output(['scrapy', 'crawl', 'mySpider', '-a', f'url={self.start_urls}', '-a', f'nbrPage={self.pages}']) return self.newsapp = Flask(__name__)data = []@app.route('/', methods=['POST'])def getNews(): mySpiderClass = ClassSpider(request.json['url'], 2) return jsonify({'data': mySpider.run()})if __name__ == "__main__": app.run(debug=True)我得到这个错误: raise not supported("unsupported url scheme %s: %s" % scrapy.exceptions.NotSupported: Unsupported URL scheme '': no handler available for that scheme当我放置 a print('my urls List: ' + str(self.start_urls))时,它会打印一个 url 列表,例如 --> my urls List: ['www.googole.com']任何帮助请
1 回答
临摹微笑
TA贡献1982条经验 获得超2个赞
我想发生这种情况是因为您首先附加url
到self.start_urls
然后用列表调用ClassSpider
srun
方法,self.start_urls
该方法又将列表附加到列表,最后得到一个嵌套列表而不是字符串列表。
为避免这种情况,您应该像这样更改__init__
方法:
def __init__(self, url, nbrPage): self.pages = nbrPage self.url = url self.start_urls = [] self.start_urls.append(url)
然后通过self.url
而不是self.start_urls
in run
:
def run(self): subprocess.check_output(['scrapy', 'crawl', 'mySpider', '-a', f'url={self.url}', '-a', f'nbrPage={self.pages}']) return self.news
添加回答
举报
0/150
提交
取消