Extracting an attribute value with beautifulsoup

前端 未结 9 2022
故里飘歌
故里飘歌 2020-11-22 04:38

I am trying to extract the content of a single \"value\" attribute in a specific \"input\" tag on a webpage. I use the following code:

import urllib
f = urll         


        
9条回答
  •  梦如初夏
    2020-11-22 05:26

    I am using this with Beautifulsoup 4.8.1 to get the value of all class attributes of certain elements:

    from bs4 import BeautifulSoup
    
    html = ""
    
    bsoup = BeautifulSoup(html, 'html.parser')
    
    for td in bsoup.find_all('td'):
        if td.has_attr('class'):
            print(td['class'][0])
    

    Its important to note that the attribute key retrieves a list even when the attribute has only a single value.

提交回复
热议问题