学习Matplotlib的时候,我也慢慢学习崔庆才的爬虫教程点击打开链接
其中有个需要利用lxml解析库的 etree.parse方法加载本地
./test.html文本文件的例子。
(为什么其中的例子的文件名写成./test.html ?? 多余的./是作什么的? 百度无果,求大神解答下!感谢!)
代码参考如下:
from lxml import etree
html = etree.parse('./test.html', etree.HTMLParser())
result = etree.tostring(html)
print(result.decode('utf-8'))
运行后发生文件加载错误。
联想到上次“利用Python的 with open功能加载读取py本地当前目录文件问题”点击打开链接
于是代码中添加全路径文件地址,还是发生加载错误问题。
貌似显示文件目录地址输入方法不正确,于是再次参考error说明中的样式输入html文本地址。
from lxml import etree
html = etree.parse('D:/python3.6/scrapy/./test.html', etree.HTMLParser())
result = etree.tostring(html)
print(result.decode('utf-8'))
加载成功,解析成功。
本地测试用的 test.html 文本代码如下:
div
ul
li class=item-0a href=link1.htmlfirst itemali
li class=item-1a href=link2.htmlsecond itemali
li class=item-inactivea href=link3.htmlthird itemali
li class=item-1a href=link4.htmlfourth itemali
li class=item-0a href=link5.htmlfifth itema
ul
div