css

不包含那个类

response.css(".list-left dd:not(.page)")

获取属性和文本

img.css("a::text").extract_first()

img.css("a::attr(href)").extract_first()

获取类page-en倒数第二个节点

response.css(".page-en:nth-last-child(2)::attr(href)").extract_first()

 xpath

获取<li>标签下hre 为 link1.html 的 <a> 标签

result = html.xpath('//li/a[@href="link1.html"]')

获取最后一个 <li> 的 <a> 的 href

result = html.xpath('//li[last()]/a/@href')

 获取倒数第二个元素的内容

result = html.xpath('//li[last()-1]/a')

获取<li> 标签下的所有 <span> 标签

#result = html.xpath('//li/span')
#注意这么写是不对的:
#因为 / 是用来获取子元素的,而 <span> 并不是 <li> 的子元素,所以,要用双斜杠

result = html.xpath('//li//span')

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
  • 2021-09-27
  • 2021-10-16
猜你喜欢
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-23
  • 2022-12-23
相关资源
相似解决方案