求救 beautifulsoup 无法使用问题

2016-06-20 10:56:14 +08:00
 coolloves
之前安装一直使用没问题,昨天想着装个股票的模块,没成功
结果发现之前使用 bs 的脚本执行报错了.
我手动测试了下,发现现在 bs 现在无法解析任何网页了.

如何排查错误,我尝试重新安装了,发现没什么用.
重新安装了 python-lxml 也没什么卵用,如何解决?
多谢各位了


>>> from bs4 import BeautifulSoup
>>> html = """
... <html><head><title>The Dormouse's story</title></head>
... <body>
... <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
... <p class="story">Once upon a time there were three little sisters; and their names were
... <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
... <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
... <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
... and they lived at the bottom of a well.</p>
... <p class="story">...</p>
... """
>>> soup = BeautifulSoup(html)
>>> print soup

>>>
4119 次点击
所在节点    Python
9 条回复
liangmishi
2016-06-20 11:02:43 +08:00
soup = BeautifulSoup(html,"lxml") 试试
coolloves
2016-06-20 11:07:22 +08:00
@liangmishi 谢谢回复
>>> soup = BeautifulSoup(html,"lxml")
>>> print soup

>>>
coolloves
2016-06-20 11:12:45 +08:00
在线等.
zjb861107
2016-06-20 11:39:55 +08:00
是因为 html 不完整吗, body 和 html 都没闭合标签
xyx119
2016-06-20 12:12:15 +08:00
直接 re 模块嘛,简单直接。。。
coolloves
2016-06-20 12:42:43 +08:00
我接触 python 时间不长,刚想卸载了重新弄,bs 是 setup.py 装的,不知道怎么卸载,看了下 pip install bs 有,pip install lxml 也有,就都 uninstall 了,结果,发现可以了.
我安装 bs 是下载了源码,python setup.py install 安装的,然后 yum install python-lxml,这样使用没问题

后来出问题后,我 pip uninstall bs ,pip uninstall lxml,
然后 pip install bs,成功,但是 pip install lxml 失败,此时,我发现可以正常工作了.


再次,请教下各位,这几种安装方式,有什么区别?
yum
pip
python setup.py install
coolloves
2016-06-20 12:49:07 +08:00
@zjb861107 我也是在网上随手复制了个例子,还没仔细看,是没闭合,不过不是这个问题,应该是我 yum 装了 python-lxml,又 pip install lxml 造成的,pip uninstall lxml 后可以了.
dingyaguang117
2016-06-20 16:18:06 +08:00
推荐 xpath ,别用 bs 了
Delbert
2016-06-20 20:17:30 +08:00
@dingyaguang117 xquery 语法真心不好用,那个是针对 xml 的, HTML 这种不严格的不太好弄啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/286964

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX