from scrapy.selector import Selector htmlText="A1B1<c>C1</c><d>D<e>E</e></d>B2<c>C2</c>" selector=Selector(text=htmlText) s=selector.xpath("//a/preceding-sibling::*") print(s.extract()) s=selector.xpath("//b/preceding-sibling::*[position()=1]") print(s.extract()) s=selector.xpath("//b[position()=2]/preceding-sibling::*") print(s.extract()) ___________________________________________________________________ print(s.extract()) 程序结果: [] ['A1', '<d>D<e>E</e></d>'] ['A1', 'B1', '<c>C1</c>', '<d>D<e>E</e></d>'] ['<d>D<e>E</e></d>']
A、s=selector.xpath("//b[position()=1]/preceding-sibling::*[position()=2]")
B、s=selector.xpath("//b[position=1]/preceding-sibling::*[position=2]")
C、s=selector.xpath("//b[position=2]/preceding-sibling::*[position=1]")
D、s=selector.xpath("//b[position()=2]/preceding-sibling::*[position()=1]")
from scrapy.selector import Selector htmlText="A1B1<c>C1</c><d>D<e>E</e></d>B2<c>C2</c>" selector=Selector(text=htmlText) s=selector.xpath("//a/following-sibling::*") print(s.extract()) s=selector.xpath("//a/following-sibling::*[position()=1]") print(s.extract()) s=selector.xpath("//b[position()=1]/following-sibling::*") print(s.extract()) _____________________________________________ print(s.extract()) 程序结果: ['B1', '<c>C1</c>', '<d>D<e>E</e></d>', 'B2', '<c>C2</c>'] ['B1'] ['<c>C1</c>', '<d>D<e>E</e></d>', 'B2', '<c>C2</c>'] ['<c>C1</c>']
A、s=selector.xpath("//b[position()=1]/following-sibling::*[position()=1]")
B、s=selector.xpath("//b[position()=1]/following-sibling::*")
C、s=selector.xpath("//b/following-sibling::*[position()=1]")
D、s=selector.xpath("//b/following-sibling::*")
from scrapy.selector import Selector htmlText=''' <html> <body> <bookstore> <book> <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></body></html></price></title></book></book></book></book></book></book></bookstore> ''' selector=Selector(text=htmlText) ______________________________________________ print(s.extract()) 程序结果: ['<book id="b2">\n <title lang="chinese">学习XML</title>\n <price>39.95</price>\n</book>']
A、s=selector.xpath("//title[@lang='chinese']/parent:*")
B、s=selector.xpath("//title[lang='chinese']/parent::*")
C、s=selector.xpath("//title[@lang='chinese']/parent::*")
D、s=selector.xpath("//title[lang='chinese']/parent:*")
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!