鱼C论坛

 找回密码
 立即注册
查看: 1905|回复: 8

[已解决]用parse读取html文件出现的问题

[复制链接]
发表于 2020-7-4 10:15:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我建了一个文件hello.html :
  1. <div>
  2.     <ul>
  3.          <li class="item-0"><a href="link1.html">first item</a></li>
  4.          <li class="item-1"><a href="link2.html">second item</a></li>
  5.          <li class="item-inactive"><a href="link3.html">third item</a></li>
  6.          <li class="item-1"><a href="link4.html">fourth item</a></li>
  7.          <li class="item-0"><a href="link5.html">fifth item</a>
  8.      </ul>
  9. </div


复制代码
读取文件,代码:
  1. from lxml import etree
  2. html = etree.parse("hello.html")
  3. result = etree.tostring(html,pretty_print = ture)
  4. print(result)
复制代码
为什么会出现以下错误呢?
  1. Traceback (most recent call last):
  2.   File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38/爬虫学习5.py", line 2, in <module>
  3.     html = etree.parse("hello.html")
  4.   File "src\lxml\etree.pyx", line 3521, in lxml.etree.parse
  5.   File "src\lxml\parser.pxi", line 1839, in lxml.etree._parseDocument
  6.   File "src\lxml\parser.pxi", line 1865, in lxml.etree._parseDocumentFromURL
  7.   File "src\lxml\parser.pxi", line 1769, in lxml.etree._parseDocFromFile
  8.   File "src\lxml\parser.pxi", line 1163, in lxml.etree._BaseParser._parseDocFromFile
  9.   File "src\lxml\parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
  10.   File "src\lxml\parser.pxi", line 711, in lxml.etree._handleParseResult
  11.   File "src\lxml\parser.pxi", line 640, in lxml.etree._raiseParseError
  12.   File "hello.html", line 8
  13. lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: li line 7 and ul, line 8, column 11
复制代码
最佳答案
2020-7-4 10:19:01
本帖最后由 Twilight6 于 2020-7-4 10:21 编辑


起始标签和末尾标签不匹配,第07 行 和 09 行分别少了 </li> 和 >

  1. <div>
  2.     <ul>
  3.          <li class="item-0"><a href="link1.html">first item</a></li>
  4.          <li class="item-1"><a href="link2.html">second item</a></li>
  5.          <li class="item-inactive"><a href="link3.html">third item</a></li>
  6.          <li class="item-1"><a href="link4.html">fourth item</a></li>
  7.         <li class="item-0"><a href="link5.html">fifth item</a></li>
  8.      </ul>
  9. </div>
复制代码


而且代码中的 ture 应该改成 True 吧?




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-4 10:19:01 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-7-4 10:21 编辑


起始标签和末尾标签不匹配,第07 行 和 09 行分别少了 </li> 和 >

  1. <div>
  2.     <ul>
  3.          <li class="item-0"><a href="link1.html">first item</a></li>
  4.          <li class="item-1"><a href="link2.html">second item</a></li>
  5.          <li class="item-inactive"><a href="link3.html">third item</a></li>
  6.          <li class="item-1"><a href="link4.html">fourth item</a></li>
  7.         <li class="item-0"><a href="link5.html">fifth item</a></li>
  8.      </ul>
  9. </div>
复制代码


而且代码中的 ture 应该改成 True 吧?




小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-4 10:28:08 | 显示全部楼层
Twilight6 发表于 2020-7-4 10:19
起始标签和末尾标签不匹配,第07 行 和 09 行分别少了  和 >

可以了,原来如此
为什么最后运行代码感觉好乱,不整齐呢
  1. b'<div>\n    <ul>\n         <li class="item-0"><a href="link1.html">first item</a></li>\n         <li class="item-1"><a href="link2.html">second item</a></li>\n         <li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li>\n         <li class="item-1"><a href="link4.html">fourth item</a></li>\n         <li class="item-0"><a href="link5.html">fifth item</a></li>\n     </ul>\n </div>\n'
复制代码

还有True是规定首字母大写么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-4 10:30:40 | 显示全部楼层
HDP1008 发表于 2020-7-4 10:28
可以了,原来如此
为什么最后运行代码感觉好乱,不整齐呢


加上这个就好了:
  1. .decode('utf-8')
复制代码

Python 是严格区分大小写的,而且你 ture 字母顺序也错了哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-4 10:34:24 | 显示全部楼层
Twilight6 发表于 2020-7-4 10:30
加上这个就好了:

Python 是严格区分大小写的,而且你 ture 字母顺序也错了哈

噢噢
代码加在哪个位置
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-4 10:37:09 | 显示全部楼层
HDP1008 发表于 2020-7-4 10:34
噢噢
代码加在哪个位置

可以啦,不错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-4 10:39:44 | 显示全部楼层

就是不懂什么原理
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-4 11:08:27 | 显示全部楼层
HDP1008 发表于 2020-7-4 10:39
就是不懂什么原理

看见字符串前面有个b吗,那代表byte类型,得用decode方法解码成正常的字符串。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-4 11:15:39 | 显示全部楼层
qiuyouzhi 发表于 2020-7-4 11:08
看见字符串前面有个b吗,那代表byte类型,得用decode方法解码成正常的字符串。

搜嘎
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 06:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表