1

我试图解析来自 url 调用的 html 输出,但我发现自己在努力解决如何让它工作。

我使用以下代码:

    import urllib2
    import base64 as b64
    import lxml.html as LH

    request = urllib2.Request('http://%s%s' % (fInput[1], fInput[2]))
    base64string = b64.encodestring('%s:%s' % (fInput[3], fInput[4])).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)
    response = urllib2.urlopen(request)
    html = response.read()
    root = LH.fromstring(html)
    sibling_content = lambda x: [b.getparent().getnext().text_content() for b in root.cssselect("td b:contains('{0}')".format(x))]
    fields = ['groupList','namelist']


    for result in zip(*[sibling_content(field) for field in fields]):
        print result

我打印输出时的结果是:

('Admins', '\nme\nmyself\nirene')('guests', '\nhin\nhinself\nbacon')

适合我需要的输出是有一个这样的数组,所以我可以将它插入数据库:

['Admins', 'me','myself','nirene'],['guests', 'hin','hinself','bacon']

接下来,我发送 HTTP 请求返回的 Html:

<BODY bgcolor="#dddddd">
   <TABLE bgcolor="#dddddd" border="1">
      <TR>
         <TD valign="top"><B>MainList</B></TD>
         <TD>
            <TABLE>
               <TR>
                  <TD>
                     <TABLE bgcolor="#dddddd" border="1">
                        <TR>
                           <TD valign="top"><B>groupList</B></TD>
                           <TD>Admins</TD>
                        </TR>
                        <TR>
                           <TD valign="top"><B>namelist</B></TD>
                           <TD>
                              <TABLE>
                                 <TR>
                                    <TD>me</TD>
                                 </TR>
                                 <TR>
                                    <TD>myself</TD>
                                 </TR>
                                 <TR>
                                    <TD>irene</TD>
                                 </TR>
                              </TABLE>
                           </TD>
                        </TR>
                     </TABLE>
                     <TABLE bgcolor="#dddddd" border="1">
                        <TR>
                           <TD valign="top"><B>groupList</B></TD>
                           <TD>guests</TD>
                        </TR>
                        <TR>
                           <TD valign="top"><B>namelist</B></TD>
                           <TD>
                              <TABLE>
                                 <TR>
                                    <TD>hin</TD>
                                 </TR>
                                 <TR>
                                    <TD>hinself</TD>
                                 </TR>
                                 <TR>
                                    <TD>bacon</TD>
                                 </TR>
                              </TABLE>
                           </TD>
                        </TR>
                     </TABLE>
                  </TD>
               </TR>
            </TABLE>
         </TD>
      </TR>
   </TABLE>
</BODY>

关于如何让这个工作的任何想法?

提前致谢。

4

3 回答 3

3

我对你的问题有点困惑。你只是问怎么转

('Members', '\nme\nmyself\nirene')('Members_2', '\nhin\nhinself\nbacon')

进入

['Members', 'me','myself','nirene'],['Members_2', 'hin','hinself','bacon']

这很容易:

>>> x = [('Members', '\nme\nmyself\nirene'), ('Members_2', '\nhin\nhinself\nbacon')]
>>> [[y[0]] + y[1].splitlines()[1:] for y in x]

仅供参考,您应该看看requestsPython 模块。而不是所有的urllib2摘要,它让你只写

requests.get(url, auth=(user, pass))
于 2013-01-10T13:14:35.927 回答
1

使用xpath

root=LH.fromstring(html)
[t.xpath('.//td[not(contains(.,"\n"))]/text()')
    for t in root.xpath('.//table[@bgcolor="#dddddd"]')]

出去:

[['Admins', 'me', 'myself', 'irene'], ['guests', 'hin', 'hinself', 'bacon']]
于 2013-01-10T14:32:39.250 回答
0

我认为只是从文本内容中去除空格就可以了。因此,在您的代码中,您可以在此行中添加一个条形调用:

兄弟内容 = lambda x: [b.getparent().getnext().text_content() .strip() for b in root.cssselect("td b:contains('{0}')".format(x))]

于 2013-01-10T13:14:11.647 回答