Search code examples
python-2.7urllib2

how to get particular tag data from url in python from urllib2


I'm very new to python 2.7 and I have a task to read a table in the URL.

I'm getting the data from URL with table. and now the issue is, I need only data but I am getting with tags also. Please help me. Thank you in advance.

from bs4 import BeautifulSoup
import urllib2


    response = urllib2.urlopen('https://www.somewebsite.com/')
    html = response.read()
    soup = BeautifulSoup(html)

    tabulka = soup.find("table", {"class" : "defaultTableStyle tableFontMD tableNoBorder"})



    records = [] 
    for row in tabulka.findAll('tr'):
        col = row.findAll('td')

        print col 

Solution

  • you have to use .text attribute

    from bs4 import BeautifulSoup
    import urllib2
    
    
    response = urllib2.urlopen('https://www.somewebsite.com/')
    html = response.read()
    soup = BeautifulSoup(html)
    
    tabulka = soup.find("table", {"class" : "defaultTableStyle tableFontMD tableNoBorder"})
    
    
    
    records = [] 
    for row in tabulka.findAll('tr'):
        col = row.findAll('td')
    
        print [coli.text for coli in col]