Python 爬虫问题咨询 - V2EX
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 2935 次点击
    这是一个创建于 2692 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小白虚心求教一个爬虫问题,对于一个已经下载好的 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     Solana     907 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 21:54 PVG 05:54 LAX 13:54 JFK 16:54
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86