Dpitt1968
Dpitt1968

Reputation: 109

Extracting data from scrapy into array

I know scrapy 'extracts()' data into an array and this is great to modify it in the pipeline I need it to stay in an array.

Unfortunately when I pull data like this -

In [8]: response.xpath('//*[@id="contacted-hosts"]//tr[1]/td[1]/text()').extract()[0]
Out[8]: u'98.139.135.129'

I need to get Out[8]: u'98.139.135.129 back in an array like this --

[u'98.139.135.129']

Hah, how do I do that? I can't find anything like this in the forum... thanks!

Ok, where do I put the []

item["Attribute"][0]['value']  = response.xpath('//*[@id="contacted-hosts"]//tr[1]/td[1]/text()').extract()[0]

Upvotes: 1

Views: 381

Answers (1)

jordanm
jordanm

Reputation: 34964

The result of your expression is a string. The [] symbols are used to create an array:

foo = ['somestring']

For your specific example:

foo = [response.xpath('//*[@id="contacted-hosts"]//tr[1]/td[1]/text()').extract()[0]]

You can also use array slicing:

In [1]: foo = ['bar', 'baz']
In [2]: foo[:1]
Out[2]: ['bar']

Which makes your example:

response.xpath('//*[@id="contacted-hosts"]//tr[1]/td[1]/text()').extract()[:1]

Upvotes: 1

Related Questions