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 · 5386 次点击
    这是一个创建于 3240 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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  
       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  
       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   ·   我们的愿景   ·   实用小工具   ·   2625 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:28 · PVG 08:28 · LAX 17:28 · JFK 20:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.