Extracting an attribute value with beautifulsoup

前端 未结 9 1913
故里飘歌
故里飘歌 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:20

    You can try gazpacho:

    Install it using pip install gazpacho

    Get the HTML and make the Soup using:

    from gazpacho import get, Soup
    
    soup = Soup(get("http://ip.add.ress.here/"))  # get directly returns the html
    
    inputs = soup.find('input', attrs={'name': 'stainfo'})  # Find all the input tags
    
    if inputs:
        if type(inputs) is list:
            for input in inputs:
                 print(input.attr.get('value'))
        else:
             print(inputs.attr.get('value'))
    else:
         print('No  tag found with the attribute name="stainfo")
    

提交回复
热议问题