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 8361341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:40:30+00:00 2026-06-09T11:40:30+00:00

We have a code which extract the service list from our tomcat server and

  • 0

We have a code which extract the service list from our tomcat server and insert it into a database. Basically, the code will receive a huge output ( 100+ results ) from a html result with the following structure:

<TABLE bgcolor=#dddddd border=1>
<TR>
<TD valign="top"><B>name</B></TD>
<TD>Loader</TD>
</TR>
<TR>
<TD valign="top"><B>enabled</B></TD>
<TD>true</TD>
</TR>
<TR>
<TD valign="top"><B>loadok</B></TD>
<TD>13</TD>
</TR>
</TABLE>
<TABLE bgcolor=#dddddd border=1>
<TR>
<TD valign="top"><B>name</B></TD>
<TD>tester</TD>
</TR>
<TR>
<TD valign="top"><B>enabled</B></TD>
<TD>false</TD>
</TR>
<TR>
<TD valign="top"><B>loadok</B></TD>
<TD>13</TD>
</TR>
</TABLE>

Following, the actual code ( which works )

#!/usr/bin/env python

import psycopg2
import urllib2
import base64
import sys
import re
import lxml.html as LH

con = None

try:

    con = psycopg2.connect(database='xx', user='xx',password='xx',host='vxx')
    cur = con.cursor()
    qry ="select iservers.env,iservers.family,iservers.prefix,iservers.iserver,iservers.login,iservers.password,services.service,"   +\
    "proxy_access.proxy_server,proxy_access.proxy_user,proxy_access.proxy_pass "        +\
    "from services,iservers,proxy_access where iservers.env='TEST' and services.id='2' "
    cur.execute(qry)
    data = cur.fetchall()

    for result in data:
    
        env      = result[0]
        family   = result[1]
        prefix   = result[2]
        iserver  = result[3]
        login    = result[4]
        password = result[5]
        service  = result[6]
        proxyHost = result[7]
        proxyUser = result[8]
        proxyPass = result[9]

        proxy_auth = "http://"+proxyUser+":"+proxyPass+"@"+proxyHost
        proxy_handler = urllib2.ProxyHandler({"http": proxy_auth})

        opener = urllib2.build_opener(proxy_handler)
        urllib2.install_opener(opener)
        request = urllib2.Request("http://"+iserver+service)
        base64string = base64.encodestring('%s:%s' % (login, password)).replace('\n', '')
        request.add_header("Authorization", "Basic %s" % base64string)
        response = urllib2.urlopen(request)
        html = response.read()
        
        ###################### CHANGE THIS TO USE A HTML PARSER
        regex = r"name</B></TD>\s<TD>(.*?)</TD>\s</TR>\s<TR>\s(.*)enabled</B></TD>\s<TD>(.*)</TD>"
        for m in re.finditer(regex,html):
            print "inserting\t"+iserver+"\t"+m.group(1)
            cur.execute("INSERT INTO pereirtc.package_status (env,family,iserver,prefix,package,status) values (%s,%s,%s,%s,%s,%s)",(env,family,iserver,prefix,m.group(1),m.group(3)))
            con.commit()
        ###################### END
except psycopg2.DatabaseError, e:
    print 'Error %s' % e
    sys.exit(1)

finally:

    if con:
        con.close()
        

After some help from Stack Overflow, I was advised to change the block on "change this …." to libxml. So I got the following block to use:

  doc = LH.fromstring(html)
  tds = (td.text_content() for td in doc.xpath("//td"))
  for td, val in zip(*[tds]*2):
      if td in ("name","enabled"):
          print (td,val)
      

With the example above, I have the result:

('name', 'Loader')
('enabled', 'true')

And what I want is, to insert the result using xpath to insert it into a database. Since I’m starting on python, I’m blocked on how I can do that with xpath/libxml.

  • 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-09T11:40:31+00:00Added an answer on June 9, 2026 at 11:40 am

    I’m not sure if you meant the following simple operation, but splitting the result into two variables is achieved as follows:

    doc = LH.fromstring(html)
    tds = (td.text_content() for td in doc.xpath("//td"))
    for td, val in zip(*[tds]*2):
        if td == "name":
            name = val
        elif td == "enabled":
            enabled = val
    
    print name
    print enabled
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample code which gets a list of values from a
I have code which has a drop down list. And when a certain option
I have code which needs to do something like this There is a list
I have code which as been working against an older Active Directory server and
I have code which performs query on Jboss server. It has JNDI based datasource
Hi I have code which sets up a basic client to server scenario and
how to extract DataTable or DataSet from Linq Query or List. e.g I have
I have the following code which is executed from the command line: import cgi,time,os,json,sys,zipfile,urllib2
I have an XML file in which I want to extract data from certain
I have the code below which is supposed to check the database for entries

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.