[英]How to access object inside parse method?
I can't figure out how to access an object inside parse
function. 我不知道如何访问parse
函数内的对象。 I want to create an object Check
which needs Product
object to be created. 我想创建一个对象Check
需要创建Product
对象的对象。 products
attribute is a list of objects which are sources of urls
. products
属性是对象列表,这些对象是urls
来源。
class GenericScraper(scrapy.Spider):
name = 'will_be_overriden'
custom_settings = {'CONCURRENT_REQUESTS': 32,
'DOWNLOAD_DELAY':0.5}
def __init__(self, occs):
super(GenericScraper,self).__init__()
self.name = products[0].site.name
self.products = products
self.xpath = self.product[0].site.xpaths.first().xpath
def start_requests(self):
for product in self.products:
yield scrapy.Request(url=product.url, callback=self.parse)
def parse(self, response):
hxs = HtmlXPathSelector(response)
text = hxs.select(self.xpath+'/text()').extract()
Check.objects.create(text=text,product=product) # CAN'T ACCESS CURRENT PRODUCT
responselog.debug(response)
Is it possible? 可能吗?
use the Request meta
attribute to communicate between callbacks. 使用Request meta
属性在回调之间进行通信。 I assume you want to associate a product
object to each request you make, so something like: 我假设您要将product
对象与您提出的每个请求相关联,所以类似:
def start_requests(self):
for product in self.products:
yield scrapy.Request(
url=product.url,
callback=self.parse,
meta={'product': product},
)
def parse(self, response):
current_product = response.meta['product']
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.