Extracting an attribute value with beautifulsoup

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

    If you want to retrieve multiple values of attributes from the source above, you can use findAll and a list comprehension to get everything you need:

    import urllib
    f = urllib.urlopen("http://58.68.130.147")
    s = f.read()
    f.close()
    
    from BeautifulSoup import BeautifulStoneSoup
    soup = BeautifulStoneSoup(s)
    
    inputTags = soup.findAll(attrs={"name" : "stainfo"})
    ### You may be able to do findAll("input", attrs={"name" : "stainfo"})
    
    output = [x["stainfo"] for x in inputTags]
    
    print output
    ### This will print a list of the values.
    

提交回复
热议问题