Python - Getting all links from a div having a class

前端 未结 4 1332
萌比男神i
萌比男神i 2021-02-03 11:09

I am using BeautifulSoup to get all links of mobile phones from this url http://www.gsmarena.com/samsung-phones-f-9-0-p2.php

My code for the following is :



        
4条回答
  •  暖寄归人
    2021-02-03 11:37

    Taken from http://www.crummy.com/software/BeautifulSoup/download/2.x/documentation.html:

    For instance, if you wanted to get only "a" Tags that had non-empty "href" attributes, you would call soup.fetch('a', {'href':re.compile('.+')}). If you wanted to get all tags that had an "width" attribute of 100, you would call soup.fetch(attrs={'width':100}).

    Try this: data = soup.findAll('div',attrs={'class':re.compile('.+')});

    Should fetch all the divs with a class property present and not empty.

提交回复
热议问题