Extracting an attribute value with beautifulsoup

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

    .find_all() returns list of all found elements, so:

    input_tag = soup.find_all(attrs={"name" : "stainfo"})
    

    input_tag is a list (probably containing only one element). Depending on what you want exactly you either should do:

    output = input_tag[0]['value']
    

    or use .find() method which returns only one (first) found element:

    input_tag = soup.find(attrs={"name": "stainfo"})
    output = input_tag['value']
    

提交回复
热议问题