from scrapy.selector import Selector htmlText=''' ...
from scrapy.selector import Selector htmlText=''' <bookstore> <book id="b1"> <title lang="english">Harry Potter</title> <price> 29.99 </price> </book> <book id="b2"> <title lang="chinese">学习XML</title> <price> 39.95 </price> </book> </bookstore> ''' selector=Selector(text=htmlText) s=selector.xpath("//book/price") print(type(s),s) ____________________________________ print(type(s),s) s=selector.xpath("//book/price").extract_first() print(type(s),s) 程序结果: <class 'scrapy.selector.unified.selectorlist'> [ <selector xpath="//book/price" data="<price>29.99</price>"> , <selector xpath="//book/price" data="<price>39.95</price>"> ] <class 'list'> [' <price> 29.99 </price>', ' <price> 39.95 </price>'] <class 'str'> <price> 29.99 </price>
A、s=selector.xpath("/book/price").extract()
B、s=selector.xpath("/book//price").extract()
C、s=selector.xpath("//book//price").extract()
D、s=selector.xpath("//book/price").extract()