andyonlines
andyonlines
采纳率60%
2019-09-20 15:38 阅读 3.2k

Python BeautifulSoup获取属性值怎么?

代码如下:

 html='''
    <img src = "//s3plus.meituan.net/v1/mss_e2821d7f0cfe4ac1bf9202ecf9590e67/cdn-prod/file:5788b470/image/loading_2.e3d934bf.png"alt = "hah"class ="poster-default"/>
<img data-src = "https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c"alt = "abc" class ="board-img"/>'''

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,"lxml")
print(soup.prettify())
print(soup.findAll(attrs='data-src'))
输出的list为空
[]

我想用beautifulsoup 来获取data-src这个属性的值,也就是这个 "https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c"

各位大神,**如果 不用 beautifulsoup 的 CSS 选择器**,怎么获取?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    helloexp helloexp 2019-09-20 15:53
    from bs4 import BeautifulSoup
    
    html='<img src = "//s3plus.meituan.net/v1/mss_e2821d7f0cfe4ac1bf9202ecf9590e67/cdn-prod/file:5788b470/image/loading_2.e3d934bf.png"alt = "hah"class ="poster-default"/><img data-src = "https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c"alt = "abc" class ="board-img"/>'
    
    soup=BeautifulSoup(html,'lxml')
    
    imgs=soup.select('img')
    #print(imgs)
    url=imgs[1]['data-src']
    print(url)
    
    点赞 评论 复制链接分享

相关推荐