美文网首页
beautifulsoup库基本使用

beautifulsoup库基本使用

作者: Challis | 来源:发表于2018-10-26 17:27 被阅读0次
    # 安装: pip install bs4
    from bs4 import BeautifuSoup
    soup = BeautifuSoup(html,'lxml')
    
    # 获取html中的所有文字
    text = soup.get()
    
    # 通过指定标签名字获取内容
    text = soup.select('.hehe')[0].get_text()    # 获取class名字是hehe的第一个的文本内容,推荐使用get_text()的方法
    text = soup.select('.heh')[0].string        # 有的时候会获取不到内容,不太推荐使用
    img_url = soup.select('img.middle')[0].get('src')
    
    
    

    相关文章

      网友评论

          本文标题:beautifulsoup库基本使用

          本文链接:https://www.haomeiwen.com/subject/obdltqtx.html