BLACKMAMBA
BLACKMAMBA

Reputation: 725

Extracting data from table on web page

I am trying to extract data from a table on a web page with beautiful soup. I want to get the data inside the cells for each row.

I am new to python have tried the following snippet, but it's not working:

import urllib.request
fname = r"C:\Python34\page.htm"
HtmlFile = open(fname, 'r', encoding='utf-8')
source_code = HtmlFile.read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(source_code, 'html.parser')
table = soup.find( "table", {"title":"geoip-demo-results-tbody"} )
rows=list()
for row in table.findAll("tr"):
   rows.append(row)
for tr in rows:
    cols = tr.findAll('td')
    p = col[0].string.strip()
    d = col[1].string.strip()
    print(p)
    print(d)

EDIT:Im getting this error Traceback (most recent call last): File "C:\Python34\scrip.py", line 14, in d = cols[1].text.strip() IndexError: list index out of range" for the row 84.78.229.78ESSantander,
Cantabria,
Cantabria,
Sp‌​ain,
Europe3900143.4647,
-3.8044Orange EspanaOrange Espana this is the html file which generated the above error www.pastebin.com/tQ3Cp5Wj thanks

Upvotes: 0

Views: 3984

Answers (1)

Vikas Ojha
Vikas Ojha

Reputation: 6950

fname = r"F:\Vikas\jobs\temp\page.htm"
HtmlFile = open(fname, 'r', encoding='utf-8')
source_code = HtmlFile.read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(source_code, 'html.parser')

table = soup.find('tbody', id='geoip-demo-results-tbody')
rows = table.find_all('tr')
for tr in rows:
    cols = tr.find_all('td')
    p = cols[0].text.strip()
    d = cols[1].text.strip()
    print(p)
    print(d)

Upvotes: 1

Related Questions