风火编程--python爬虫几个xpath解析方法

python爬虫解析xpath

requests获取的响应体

from lxml import etree
html = etree.HTML(response.text) # 二进制类型用.content
result = html.xpath(“expression”),
返回list, 一个用[0]

selenium获取的响应体

result = response.xpath(“expression”).xtract(),
返回list, 一个用extract_first()

一些特殊的解析

    # 根据文本内容取标签的下一个弟标签的文本
     driver.find_elements_by_xpath(' /span[text()="持续时间:"]/following-sibling::*[1]/text()')
     # 上一个节点     preceding-sibling::a/ttext()
     # 父节点    /parent::li/text()
     # 祖先节点 /ancestor::*[2]/text()
  # 模糊查询
driver.find_elements_by_xpath("//span[contains(@class,'center_close')]")
# 将节点下的所有文本拼接成字符串
string(node())
#取节点下的第一个文本
string(node()/text())

解析式的或连接

html.xpath('//tr[@class="odd"] | //tr[@class="even"]') 

或者

 html.xpath('//tr[@class="even" or @class="odd"]')

不是

 html.xpath('//tr[not(@class="even")]')

参考链接

https://www.cnblogs.com/songshu120/p/5182043.html

你可能感兴趣的:(python应用)