Notebook
  • Study hard and make progress every day
  • Mouka
    • Windows Internal
      • Helper Functions(todo:)
      • Find Kernel Module Address
      • Patch Guard Oops
      • Hook SSDT(Shadow)
      • Restore SSDT(Shadow)
      • Misc
        • Volatile in C
    • AntiCheat
      • Inject Defense
      • Injection Method
    • DriverDevelopment
      • 20180625
      • 20180626-27
    • Python
      • Python Tricks
        • 内置 http 服务器
        • 函数作为变量
        • "is" vs "=="
        • 直接变量值交换
        • 计算代码执行时间
        • 函数参数分解
        • 打印Python字典
        • 命名元组代替class
        • get()方法访问字典
        • 字典排序
        • 一次检查多个标志
        • 合并两个字典
        • re.sub使用替换函数
    • Algorithms
      • Greedy
        • 使括号平衡的最小交换次数
        • 埃及分数
      • DynamicProgramming
        • 0-1 背包问题
      • LeetCode
        • Count Primes
  • Honey
    • Python笔记
      • lxml库
      • os库
      • json文件读写
      • Scrapy
        • Scrapy安装与开始项目
        • Scrapy-Xpath
Powered by GitBook
On this page
  1. Honey
  2. Python笔记

lxml库

  • lxml可直接读取html文件,不需要其他操作

  • lxml需要自己的编译器来解析html文件

parser = etree.HTMLParser(encoding="gbk")
html = etree.parse(html, parser=parser)  # 输入为html文件,增加解析器
  • lxml返回的结果需要利用tostring将单个对象转换为字节(不能直接对list使用此方法),再利用decode解码为文字。

answers = html.xpath('//*/div[@class="l-tbody"]/div[@class="art-text"]/p')
answer = ""
for i in answers:
    i = etree.tostring(i, encoding='gbk')
    i = i.decode('gbk')
    i = re.sub(r' |\n|\r|\t|&#13|;|', "", i)
    answer = answer + "\n" + re.sub(r'<.*?>', front, i)
PreviousPython笔记Nextos库

Last updated 6 years ago