run = scrapy.Request('https://httpbin.org/ip',callback=self.proxy_check_available, meta=meta, dont_filter=True,errback = lambda x: self.download_errback(x,i['proxy']))
def proxy_check_available(self, response):
json_data = json.loads(response.text)['origin']
print(json_data)
proxy_ip = response.meta['_proxy_ip']+', '+response.meta['_proxy_ip']
print('proxy_ip:'+proxy_ip)
if proxy_ip == json.loads(response.text)['origin']:
return True
else:
return False
我希望能夠把True或False的值回傳到第一個function
想請問應該如何呼叫?
網路上查好像只查得到由第一個function傳值給callback function
卻沒有倒過來的例子。
剛剛幫你看了一下文件,我自己是沒用過scrapy
def proxy_check_available(self, response):
json_data = json.loads(response.text)['origin']
print(json_data)
proxy_ip = response.meta['_proxy_ip']+', '+response.meta['_proxy_ip']
print('proxy_ip:'+proxy_ip)
if proxy_ip == json.loads(response.text)['origin']:
response.meta["proxy_check_available"] = True
else:
response.meta["proxy_check_available"] = False
Request物件get以後,永遠是回傳Response物件,不過好像你可以透過callback改變Response物件的meta。
run = scrapy.Request('https://httpbin.org/ip',callback=self.proxy_check_available, meta=meta, dont_filter=True,errback = lambda x: self.download_errback(x,i['proxy']))
print(run.meta["proxy_check_available"])