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

如何用scrapy抓取数据时使用Xpath获得全部文本

  •  
  •   brucebot · 2013-08-30 22:37:30 +08:00 · 6953 次点击
    这是一个创建于 4103 天前的主题,其中的信息可能已经有所发展或是发生改变。
    使用的是
    item['description'] = app.select('./li//text()').extract_unquoted()
    得到
    {"description": ["\n I visited the local ", "robotic", " club and recorded the preparation for the up-coming competition. I was invited to make a speech there\u00a0", "...", "\n "]

    请教应该怎么写,才能得到
    {"description": ["I visited the local robotic club and recorded the preparation for the up-coming competition. I was invited to make a speech there...“]
    这样的形式,就是只需要保留文本的部分?
    4 条回复    1970-01-01 08:00:00 +08:00
    beordle
        1
    beordle  
       2013-08-30 22:38:48 +08:00   ❤️ 1
    "".join(app.select('./li//text()').extract_unquoted())
    brucebot
        2
    brucebot  
    OP
       2013-08-30 22:45:24 +08:00
    @beordle 这样出来的结果是
    {"description": "\n I visited the local robotic club and recorded the preparation for the up-coming competition. I was invited to make a speech there...\n“
    []不见了?
    beordle
        3
    beordle  
       2013-08-30 22:56:25 +08:00   ❤️ 1
    加上就好了嘛~
    item['description'] = ["".join(app.select('./li//text()').extract_unquoted())]
    如果需要/n换行的话
    item['description'] = "".join(app.select('./li//text()').extract_unquoted()).split('\n')
    可能还有一些细节问题。hack下即可
    brucebot
        4
    brucebot  
    OP
       2013-08-30 23:06:38 +08:00
    @beordle 非常感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1046 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 23:24 · PVG 07:24 · LAX 15:24 · JFK 18:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.