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
smallgoogle
V2EX  ›  Python

lxml 的 xpath 的一个 BUG,不知道你们遇到没?

  •  
  •   smallgoogle · 2018-11-10 18:20:40 +08:00 · 2159 次点击
    这是一个创建于 1965 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在做网易云个人信息遍历抓取;
    那么毛病就来了;
    https://music.163.com/user/home?id=29879272
    https://music.163.com/user/home?id=132128

    这是两个个人主页; 29879272 这个呢,etree.HTML(源码),可以完全解析 html。

    132128 这个呢,就 BUG 了,etree.HTML(源码),发现 html 被截断。

    会被源码里面的 description 的 —— 双横线给截断了。简直奇葩;

    有没有大神 look look。我发现这是一个 BUG

    7 条回复    2018-11-14 17:22:25 +08:00
    itskingname
        1
    itskingname  
       2018-11-10 18:35:44 +08:00 via iPhone
    lxml 不要用 etree.HTML。换成

    from lxml.html import fromstring

    selector = fromstring(source)
    selector.xpath(...)
    smallgoogle
        2
    smallgoogle  
    OP
       2018-11-10 19:31:09 +08:00
    @itskingname 一样的。都会被那个双横线给截断掉。
    ioven
        3
    ioven  
       2018-11-11 08:36:26 +08:00
    >音乐人。不定义,不局限。 —\\u0000 —\\u0000 微博

    被零字符截断了,替换掉就行
    smallgoogle
        4
    smallgoogle  
    OP
       2018-11-12 12:52:45 +08:00
    @ioven 我是不是要先替换,才行。
    ioven
        5
    ioven  
       2018-11-12 15:04:42 +08:00
    @smallgoogle selector = fromstring(source.replace('\u0000', ''))

    之后正常使用
    rocketman13
        6
    rocketman13  
       2018-11-13 19:24:50 +08:00
    最近处理 PostgreSQl 数据库写入时也遇到过\u0000 不能解析,字符串中 replace 掉就好了
    canwushuang
        7
    canwushuang  
       2018-11-14 17:22:25 +08:00
    python 模块采用 c 编写导致,而\u0000 是 unicode 表示的一个特殊字符,,在 c 里面用这个字符作为字符串结束的标志。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5341 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:48 · PVG 13:48 · LAX 22:48 · JFK 01:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.