andyonlines 2019-09-20 15:38 采纳率: 50%
浏览 3699
已采纳

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条回答 默认 最新

  • 阿雷由 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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?