sel = Selector(text=a.text)
xpath:
1获取属性值:@+属性值
tags = sel.xpath('//div[contains(@class,"goodsItem")]/a/img/@src').extract()
2获取文本值:text()
tags = sel.xpath("//div[@class='goodsItem']/font/text()").extract()
CSS选择器:
1获取属性值:
teacher_info = sel.css('img[src$=".jpg"]').xpath('@src').extract()
teacher_info = sel.css('img[src$=".jpg"]::attr(src)').extract()
2获取文本值:
teacher_info=sel.css('.goodsItem > p a::text').extract()
本文探讨了如何使用XPath和CSS选择器从HTML文档中有效地提取信息。通过示例展示了如何获取属性值和文本内容,例如图片源路径和页面文本。这些技术在前端开发、网页抓取和数据解析等领域具有广泛应用。
1328

被折叠的 条评论
为什么被折叠?



