scrapy/scrapy — pull request #569 by darkrho (2014-01-29 22:21:07+00:00)
Title: [WIP] Add .get(index) method to selector results.

This is a proposal for #568.

It is very common when not using the item loaders to want to extract just one value using the selector (i.e.: an href value), but the current pattern is either to write a try/except block or checking if the returned result is empty before attempting to access the first element.

This pull requests adds the method `.get(index)` to the class `SelectorList` and adds a new class `ResultList` to provide the same method for the output of `.extract()` and `.re(regex)`. Optionally, it could allow to pass a default value just like `dict.get`, `operator.getattr` or `operator.getitem`.

If the proposal looks good I can the proper test cases.

Here is an usage example:

```python
In [1]: text = """
...: foo
...: 1234
...: bar
...: 5678
...: """

In [2]: sel = Selector(text=text)

In [3]: sel.css('span').get(0)
Out[3]: foo'>

In [4]: sel.css('h1').get(0)

In [5]: sel.css('span').re('\d+').get(1)
Out[5]: u'5678'

In [6]: sel.css('span').get(3)
Out[6]: 5678'>
```