python - xpath解析使用extract()的时候,几种情况有点分不清楚
黄舟
黄舟 2017-04-17 18:00:29
0
2
962

xpath解析使用extract()的时候,几种情况有点分不清楚,如下截图所示:

什么时候用哪种?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
Ty80
  1. 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__()
  2. Returns a list (the one that comes with the system) which contains some of the content you extracted

  3. Returns the first element of the list in 2 (throws an exception if the list is empty)

  4. 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

  5. 4 returns a str (if Python2 is unicode), so 5 will return the first character of str

迷茫

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template