爬虫技术-- BeautifulSoup 标签选择的一个疑惑 - 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
redhatping
V2EX    Python

爬虫技术-- BeautifulSoup 标签选择的一个疑惑

  •  
  •   redhatping 2015-06-14 10:38:46 +08:00 5961 次点击
    这是一个创建于 3804 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from bs4 import BeautifulSoup import re html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <table> <tr> <th> biaoti </th> <td> hi </td> </tr> </table> <table> <tr> <th> biaoti2 </th> <th> biaoti3 </th> </tr> <tr> <td> hello </td> <td> <a href= 'http://www.sina.com/blog'> world </a> </td> </tr> </table> </body> </html> """ soup = BeautifulSoup(html_doc) print soup.td.find_all('a') #first td ? how to chose second td print soup.find_all(href = re.compile("blog")) 

    用第二种方法, 可以方便找到 <a>标签,

    如果使用第一种方法,明显是找第一个td, 当然没有a了,有没有其他办法可以选择呢? 不使用re的情况下。

    另外:v2ex Markdown- 怎么让python 高亮啊。。

    21 条回复    2015-06-15 10:21:34 +08:00
    lxy
        1
    lxy  
       2015-06-14 10:54:03 +08:00
    通过点取属性的方式只能获得当前名字的第一个tag。直接 soup.find_all('a') 就能找到所有 a 标签了。官网有详细中文文档。
    redhatping
        2
    redhatping  
    OP
       2015-06-14 11:01:04 +08:00
    @lxy 是这样的, 我的意思是: 我想找 td 下 有 <a>标签的方法?
    如果不是td下的, 不需要。
    lxy
        3
    lxy  
    /div>   2015-06-14 11:10:45 +08:00
    我想到的方法是 link = soup.find_all('a'),然后循环判断 link[i].parent.name 是否为 td
    redhatping
        4
    redhatping  
    OP
       2015-06-14 11:13:18 +08:00
    @lxy 谢谢, 是 一个思路,有没有高大快的方法呢。。各位
    bianzhifu
        5
    bianzhifu  
       2015-06-14 11:25:05 +08:00
    我更喜欢pyquery
    zeroten
        6
    zeroten  
       2015-06-14 12:23:51 +08:00
    感觉pyquery更顺手些
    zztt168
        7
    zztt168  
       2015-06-14 13:18:00 +08:00 via iPhone
    谢谢@bianzhifu @zeroten 推荐pyquery。
    imn1
        8
    imn1  
       2015-06-14 13:32:49 +08:00
    一直 lxml + xpath……

    其实一直正则,偶尔 lxml
    Tink
        9
    Tink  
    PRO
       2015-06-14 13:37:05 +08:00
    xpath好用
    ca1n
        10
    ca1n  
       2015-06-14 14:01:42 +08:00
    @lxy
    @redhatping
    findall['td'].findall['a'] 多看文档
    redhatping
        11
    redhatping  
    OP
       2015-06-14 16:07:27 +08:00
    @ca1n 呵呵,错误的哦
    AttributeError: 'ResultSet' object has no attribute 'find_all'
    对象可没有这个属性
    realityone
        12
    realityone  
       2015-06-14 16:14:21 +08:00
    @redhatping
    @lxy
    findAll
    redhatping
        13
    redhatping  
    OP
       2015-06-14 16:58:50 +08:00
    @realityone 只有find_all 和 find方法 ,没有findall 。
    realityone
        14
    realityone  
       2015-06-14 17:26:24 +08:00
    @redhatping 啊。。已经是 bs4 了啊。。
    xjx0524
        15
    xjx0524  
       2015-06-14 17:30:05 +08:00
    @imn1
    @Tink
    挺喜欢用xpath的,但是有个问题不知道你们怎么做的
    假如有个元素确定只有一个,那可以直接用a[0]
    但是在批量爬取网页时,有可能某个网页没有这个元素,我一般都会
    if a:
    ____a[0] balabala
    这样判断,但是这种情况多了代码会很难看。
    不知道怎么处理比较好?
    ca1n
        16
    ca1n  
       2015-06-14 21:09:31 +08:00
    @redhatping 非要把代码甩在你脸上才开心

    >>> for i in bs(html).find_all('td'):
    ... for x in i.find_all('a'):
    ... print x

    拿好不送
    ca1n
        17
    ca1n  
       2015-06-14 21:14:38 +08:00   1
    for i in bs(html).find_all('td'):
    ....for x in i.find_all('a'):
    ........print x

    上面格式乱了, 如果你要说这个不能运行那纯粹是你自己的问题了
    redhatping
        18
    redhatping  
    OP
       2015-06-14 21:58:51 +08:00
    @ca1n 当然可以,
    runningteeth
        19
    runningteeth  
       2015-06-14 22:30:36 +08:00
    secondwtq
        20
    secondwtq  
       2015-06-14 23:08:59 +08:00
    纯路过... 这两天撸前端,Polymer blablabla,看到这个主题第一反应居然是 document.querySelector...

    以前 soup 和 pyquery 都用过一点,pyquery 用起来挺顺手,不过我记得貌似有坑,好像是换行处理不对还是什么来着...
    imn1
        21
    imn1  
       2015-06-15 10:21:34 +08:00
    @xjx0524
    这个没什么问题,空列表返回false,这种写法合理
    嫌不好看,可以 var=findall(...); if var: 判断
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3445 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 04:45 PVG 12:45 LAX 20:45 JFK 23:45
    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