V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
coolloves
V2EX  ›  Python

求救 beautifulsoup 无法使用问题

  •  
  •   coolloves · 2016-06-20 10:56:14 +08:00 · 4104 次点击
    这是一个创建于 2859 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前安装一直使用没问题,昨天想着装个股票的模块,没成功
    结果发现之前使用 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

    >>>
    第 1 条附言  ·  2016-06-20 12:50:09 +08:00
    应该是我 yum 装了 python-lxml,
    又 pip install lxml 造成的,pip uninstall lxml 后可以了.

    昨天 pip install tushare 的时候提示没有 lxml,所以我 pip install lxm 了,当时忘记有什么特殊情况了,反正装 pip lxml 也折腾了一阵子
    9 条回复    2016-06-20 20:17:30 +08:00
    liangmishi
        1
    liangmishi  
       2016-06-20 11:02:43 +08:00
    soup = BeautifulSoup(html,"lxml") 试试
    coolloves
        2
    coolloves  
    OP
       2016-06-20 11:07:22 +08:00
    @liangmishi 谢谢回复
    >>> soup = BeautifulSoup(html,"lxml")
    >>> print soup

    >>>
    coolloves
        3
    coolloves  
    OP
       2016-06-20 11:12:45 +08:00
    在线等.
    zjb861107
        4
    zjb861107  
       2016-06-20 11:39:55 +08:00
    是因为 html 不完整吗, body 和 html 都没闭合标签
    xyx119
        5
    xyx119  
       2016-06-20 12:12:15 +08:00
    直接 re 模块嘛,简单直接。。。
    coolloves
        6
    coolloves  
    OP
       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
        7
    coolloves  
    OP
       2016-06-20 12:49:07 +08:00
    @zjb861107 我也是在网上随手复制了个例子,还没仔细看,是没闭合,不过不是这个问题,应该是我 yum 装了 python-lxml,又 pip install lxml 造成的,pip uninstall lxml 后可以了.
    dingyaguang117
        8
    dingyaguang117  
       2016-06-20 16:18:06 +08:00 via iPhone
    推荐 xpath ,别用 bs 了
    Delbert
        9
    Delbert  
       2016-06-20 20:17:30 +08:00 via Android
    @dingyaguang117 xquery 语法真心不好用,那个是针对 xml 的, HTML 这种不严格的不太好弄啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1088 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:53 · PVG 06:53 · LAX 15:53 · JFK 18:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.