> For the complete documentation index, see [llms.txt](https://m0uk4.gitbook.io/notebooks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://m0uk4.gitbook.io/notebooks/honey/python-notes/lxml-ku.md).

# lxml库

* lxml可直接读取html文件，不需要其他操作
* lxml需要自己的编译器来解析html文件

```python
parser = etree.HTMLParser(encoding="gbk")
html = etree.parse(html, parser=parser)  # 输入为html文件，增加解析器
```

* lxml返回的结果需要利用`tostring`将单个对象转换为字节（不能直接对list使用此方法），再利用`decode`解码为文字。

```python
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)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://m0uk4.gitbook.io/notebooks/honey/python-notes/lxml-ku.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
