xpath解析使用extract()的时候,几种情况有点分不清楚,如下截图所示:
什么时候用哪种?
PHP中文网2017-04-17 18:02:29
Returns a SelectorList object http://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/selectors.html#selectorlist
SelectorList 类是内建 list 类的子类,提供了一些额外的方法:
xpath(query)
css(query)
extract()
re()
__nonzero__()
Returns a list (the one that comes with the system) which contains some of the content you extracted
Returns the first element of the list in 2 (throws an exception if the list is empty)
Returns the first element in the SelectorList in 1 (throws an exception if the list is empty), which is consistent with the effect achieved in 3
4 returns a str (if Python2 is unicode), so 5 will return the first character of str
迷茫2017-04-17 18:02:29
It is best to use the first method. After extraction, the selector object will be converted into a list type. It is not recommended to use extract()[0] directly, because sometimes a list index error will be reported