标签 xpath 下的文章

解决办法

logger.info('点击下翻月份')
xiayiye = self.browser.find_element_by_xpath(
    '//div[@class="mp-direct mp-direct-next" and @mp-role="nextMonthBtn" and @style="display: block;"]/a')
# 点击
# xiayiye.click()  # Message: element not interactable
self.browser.execute_script(
    "arguments[0].click();", xiayiye)

使用scrapy框架写爬虫的时候,直接爬取request.body的话,会包含<script>标签的代码,如果想排除<script>的标签内容,那么写法如下:
items['body'] = response.xpath('normalize-space(string(/html/body//*[not(self::script)]))')[0].extract()
如果还想排除其他标签:
items['body'] = response.xpath('normalize-space(string(/html/body//*[not(self::script or self::img or self::div)]))')[0].extract()

XPath轴(XPath Axes)可定义某个相对于当前节点的节点集:

  1. ancestor 选取当前节点的所有先辈(父、祖父等)。
  2. ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。
  3. attribute 选取当前节点的所有属性。
  4. child 选取当前节点的所有子元素。
  5. descendant 选取当前节点的所有后代元素(子、孙等)。
  6. descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。
  7. following 选取文档中当前节点的结束标签之后的所有节点。
  8. namespace 选取当前节点的所有命名空间节点。
  9. parent 选取当前节点的父节点。
  10. preceding 选取文档中当前节点的开始标签之前的所有节点。
  11. preceding-sibling 选取当前节点之前的所有同级节点。
  12. self 选取当前节点。

参考链接:

  1. http://www.w3school.com.cn/xpath/index.asp
  2. https://stackoverflow.com/questions/38199646/xpath-flatten-text-excluding-certain-nodes

注意:实际过滤结果并没有那么完美!还存在一部分未过滤掉的!