Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:09:21+00:00 2026-06-14T23:09:21+00:00

I try to read data from a table in html. I read periodically and

  • 0

I try to read data from a table in html. I read periodically and the table length always change and I don’t know its length. However the table is always on the same format so I try to recognize some pattern and read data based on it’s position.
The html is of the form:

<head>
<title>Some webside</title>
</head>
<body 
<tr><td> There are some information coming here</td></tr>
<tbody><table>
<tr><td><a href="d?k=101">First</a></td><td>London</td><td>24</td><td>3</td><td>19:00</td><td align="center"></td></tr>
<tr bgcolor="#cccccc"><td><a href="d?k=102">Second</a></td><td>NewYork</td><td>24</td><td>4</td><td>20:13</td><td align="center"></td></tr>
<tr><td><a href="d?k=201">Some surprise</a></td><td>Swindon</td><td>25</td><td>5</td><td>20:29</td><td align="center"></td></tr>
<tr bgcolor="#cccccc"><td><a href="d?k=202">Third</a></td><td>Swindon</td><td>24</td><td>6</td><td>20:45</td><td align="center"></td></tr>
</tbody></table>
<tr><td> There are some information coming here</td></tr>
</body>

I convert html to a string and go over it to read the data but I want to read it only once. My code is:

def ReadTable(m):
    refList = []
    firstId = 1
    nextId = 2
    k = 1
    helper = 1
    while firstId != nextId:
        row = []
        helper = m.find('<td><a href="d?k=', helper) + 17
        end_helper = m.find('">', helper)
        rowId = m[helper : end_helper]
        if k == 1:          # to check if looped again
            firstId = rowId
        else:
            nextId = rowId      
        row.append(rowId)
        helper = end_helper + 2
        end_helper = m.find('</a></td><td>', helper)
        rowPlace = m[helper : end_helper]
        row.append(rowPlace)
        helper = m.find('</a></td><td>', end_helper) + 13
        end_helper = m.find('</td><td>', helper)
        rowCity = m[helper : end_helper]
        row.append(rowCity)
        helper = end_helper + 9
        end_helper = m.find('</td><td>', helper)
        rowDay = m[helper : end_helper]
        row.append(rowDay)
        helper = end_helper + 9
        end_helper = m.find('</td><td>', helper)
        rowNumber = m[helper : end_helper]
        row.append(rowNumber)
        helper = end_helper + 9
        end_helper = m.find('</td>', helper)
        rowTime = m[helper : end_helper]
        row.append(rowTime)
        refList.append(row)
        k +=1
    return refList

if __name__ == '__main__':
    filePath = '/home/m/workspace/Tests/mainP.html'
    fileRead = open(filePath)
    myString = fileRead.read()
    print myString
    refList = ReadTable(myString)
    print 'Final List = %s' % refList

I expect the outcome as a list with 4 lists inside like that:

Final List = [['101', 'First', 'London', '24', '3', '19:00'], ['102', 'Second', 'NewYork', '24', '4', '20:13'], ['201', 'Some surprise', 'Swindon', '25', '5', '20:29'], ['202', 'Third', 'Swindon', '24', '6', '20:45']]

I expect that after first loop the string is read again and the firstId is found again and my while-loop will terminate. Instead I have infinite loop and my list start to look like this:

Final List = [['101', 'First', 'London', '24', '3', '19:00'], ['102', 'Second', 'NewYork', '24', '4', '20:13'], ['201', 'Some surprise', 'Swindon', '25', '5', '20:29'], ['202', 'Third', 'Swindon', '24', '6', '20:45'], ['me webside</title>\n</head>\n<body \n<tr><td> There are some information coming here</td></tr>\n<tbody><table>\n<tr><td><a href="d?k=101', 'First', 'London', '24', '3', '19:00'], ['102', 'Second', 'NewYork', '24', '4', '20:13']...

I don’t understand why my helper start to behave this way and I can’t figure out how a program like that should be written. Can you suggest a good/effective way to write it or to fix my loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:09:23+00:00Added an answer on June 14, 2026 at 11:09 pm

    You aren’t checking the return from your find and it is returning -1 when it doesn’t find a match.
    http://docs.python.org/2/library/string.html#string.find
    Return -1 on failure

    I updated this section of the code and it returns as you expect now. First and last row below match what you have above so you can find the replacement.

        row = []
        helper = m.find('<td><a href="d?k=', helper)
        if helper == -1:
            break
        helper += 17
        end_helper = m.find('">', helper)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read data from a socket, however whenever I try to read
i try to read Data from an Oracle-Database. The problem is that in some
In the following code, I try to read data from a plist: -(void)readPreferences {
I have an ssis package...which read data from fixed-column-width text file into DB table.
I try to read a text files with 20x20 data into variable C, and
I try to read out all existing calendars. I tried the example from here:
i need to retrive some data from table registration of database to show.aspx page
I'm reading data from a table( from a MySQL Database) with Hibernate SQL Query.
I want to build a service that will pass the data read from the
I am trying to read data from multiple Kinect sensors (3 at the moment)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.