search

Home  >  Q&A  >  body text

python - xpath解析使用extract()的时候,几种情况有点分不清楚

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

什么时候用哪种?

黄舟黄舟2804 days ago946

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 18:02:29

    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

    reply
    0
  • 迷茫

    迷茫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

    reply
    0
  • Cancelreply