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

Python 爬虫问题咨询

  •  
  •   frmongo · 2018-07-19 15:36:57 +08:00 · 2340 次点击
    这是一个创建于 2101 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小白虚心求教一个爬虫问题,对于一个已经下载好的 html,如果在它的内容里,一个 class 的名字里包含了空格,CSS 选择器怎么书写呢? 比如一个 html 里是这样的

    <div class="aaa">
        <div class="bbb">
            <ul class="tab ccc">
                <li>
                "Sting1"
                </li>
            </ul>
            <ul class="tab ddd">
            "string2"
            </ul>
            ...
        </div>
    <div>
    

    如果我想获得 tab ccc 下 li 包括的 String1,这样写是不行的:

    d = tree.cssselect('div.aaa > div.bbb > ul.tab ccc > li')
    print d.text_content()
    

    那我该怎么写 CSS 选择器呢?

    16 条回复    2018-07-20 11:39:09 +08:00
    14night
        1
    14night  
       2018-07-19 16:08:37 +08:00
    d = tree.cssselect('div.aaa > div.bbb > ul.tab.ccc > li')
    print d.text_content()

    试试呢?
    pcdRob
        2
    pcdRob  
       2018-07-19 16:13:45 +08:00
    body > div > div.bbb > ul.tab.ccc > li
    Phant0m
        3
    Phant0m  
       2018-07-19 16:38:20 +08:00
    chrome 开发者 审查元素, 右键复制 可以选 select 和 xpath
    alen
        4
    alen  
       2018-07-19 16:46:12 +08:00   ❤️ 1
    何不用 bs4 呢!
    xanthu
        5
    xanthu  
       2018-07-19 16:47:11 +08:00 via Android
    建议用 xpath...
    xanthu
        6
    xanthu  
       2018-07-19 16:49:17 +08:00 via Android
    chrome 有个 ChroPath 的插件,点下元素,css 和 xpath 选择器就出来了
    lhx2008
        7
    lhx2008  
       2018-07-19 16:50:31 +08:00 via Android
    用 pyqury,直接 tree(".aaa .bbb .ccc.tab li").text()
    frmongo
        8
    frmongo  
    OP
       2018-07-19 16:51:17 +08:00
    @xanthu 嗯,我研究研究,我在尝试解析一个很复杂的 html, 发现我的 selector 写的没问题,也找不到元素,奇了怪了,chrome 复制的 selector 也不好使
    MES
        9
    MES  
       2018-07-19 17:09:44 +08:00
    @frmongo 包含空格的,写一个就行,看你用哪个方便一点了。
    frmongo
        10
    frmongo  
    OP
       2018-07-19 17:17:19 +08:00
    走了一遍程序还不行,这个链接里的 http://detail.zol.com.cn/1225/1224202/param.shtml 里的字符串 Android 8.1
    死活找不到
    pp = 'body > div:nth-child(10) > div.content > div:nth-child(3) > div.detailed-parameters > table:nth-child(2) > tbody > tr:nth-child(4) > td > span'
    tanglijun
        11
    tanglijun  
       2018-07-19 17:23:25 +08:00
    d = tree.xpath("//div[@class='aaa']/div[@class='tab ccc']/li/text()")
    for i in d:
    print(d)

    用的 xpath,试试行不!
    frmongo
        12
    frmongo  
    OP
       2018-07-19 17:31:48 +08:00
    解决了,谢谢各位,我作为一个小白,也差了一些资料,现在能抓到了
    hatsuyuki
        13
    hatsuyuki  
       2018-07-19 18:13:23 +08:00
    css 选择器直接写 .class1.class2,class 以点开头选择,两个 class 直接写一起,不要加空格
    forget166
        14
    forget166  
       2018-07-19 22:23:29 +08:00
    soup.find_all('ul',{'class':'tab ccc'}).get_text()
    congeec
        15
    congeec  
       2018-07-20 03:43:27 +08:00
    div.aaa.bbb
    frmongo
        16
    frmongo  
    OP
       2018-07-20 11:39:09 +08:00
    多谢各位,赞
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   954 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:50 · PVG 03:50 · LAX 12:50 · JFK 15:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.