Python - Getting all links from a div having a class

前端 未结 4 1326
萌比男神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:46

    There are only three

    elements in that page with a class of 'makers', this will print the first link from each div, so three in all.

    This is likely closer to what you desire:

    import urllib2
    from BeautifulSoup import BeautifulSoup
    
    url = "http://www.gsmarena.com/samsung-phones-f-9-0-p2.php"
    text = urllib2.urlopen(url).read()
    soup = BeautifulSoup(text)
    
    data = soup.findAll('div',attrs={'class':'makers'})
    for div in data:
        links = div.findAll('a')
        for a in links:
            print "http://www.gsmarena.com/" + a['href']
    

提交回复
热议问题