Using beautiful soup to get html attribute value

前端 未结 2 1730
暗喜
暗喜 2021-01-24 05:05

What i\'m trying to do is use beautiful soup to get the value of an html attribute.

What i have so far is:

soup = BeautifulSoup(html, \"html.parser\")
         


        
相关标签:
2条回答
  • 2021-01-24 05:57

    I assume your find() method returns None so you can't concatenate string at line #2.

    >>> print('' + None)                                       
    Traceback (most recent call last):                         
      File "<stdin>", line 1, in <module>                      
    TypeError: can only concatenate str (not "NoneType") to str
    

    That's an error message I get in Python 3.7
    That's not the Soup problem, you have to explicitly check return value.

    Provide more information (stacktrace) if that's not the case

    0 讨论(0)
  • 2021-01-24 05:58

    you should try the following

    soup = BeautifulSoup(html, "html.parser")
    print("data-sitekey=" + soup.find("div", attrs={"class" : "data-sitekey"}))
    return soup.find("div", attrs={"class" : "data-sitekey"})
    
    0 讨论(0)
提交回复
热议问题