Python 使用BeautifulSoup拾取以“分隔”分隔的文本:&引用;

Python 使用BeautifulSoup拾取以“分隔”分隔的文本:&引用;,python,beautifulsoup,Python,Beautifulsoup,网页源代码包含如下部分: <TR> <TD width="40%">Company No. <I>(CO.)</I> : <B>056</B></TD> <TD width="40%">Country Code. <I>(CC.)</I> : <B>3532 </B></TD></TR> <TR> <TD>

网页源代码包含如下部分:

<TR>
<TD width="40%">Company No. <I>(CO.)</I> : <B>056</B></TD>
<TD width="40%">Country Code. <I>(CC.)</I> : <B>3532 </B></TD></TR>
<TR>
<TD>Register <I>(Reg.)</I> : <B>FD522</B></TD>
<TD>Credit<I>(CD.) </I>: <B>YES</B></TD></TR>
<TR>
<TD>Type <I>(TP.)</I> : <B>PRIVATE</B></TD></TR>
但它的效果并不好。它在一行中返回“Company No.(CO.):056”,但我想将它们分开,比如“Company No.”、“CO.”和“056”

我还尝试:

all_texts = soup.find_all(":")
或:

等等,它们不起作用

结果 下面的帮助解决了两个问题。放在那里供参考:

这样可以获得粗体字母的内容,但在某些句子中,最后一个字母缺失:

for bb in aa:
    cc = bb.get_text()
    dd = cc[cc.find("<b>")+1 : cc.find("</b>")]
    print dd

使用findAll获取完整HTML文档的正确部分,然后使用:

text = soup.get_text()
print text
然后使用“.split()”将其拆分为数组


您不必强迫自己使用beautifulsoup函数来分隔它们 因为里面的每个数据都有不同的令牌密钥要分割 即:

公司编号(公司):056
  • 公司编号。以“.”分隔
  • (公司)其名称以“:”分隔
  • 056内部
  • 我建议您使用子字符串方法从每个td获取数据:

    #grab all td
    all_texts = soup.findAll("td") 
    for txt in all_texts
            #convert td into string
            td = str(td)
            txt1 = td[td.find(">")+1 : td.find("<i>")] #get first data from <td>...</i>
            txt2 = td[td.find("<i>")+3 : td.find("</i>")] #get 2nd data from <i>...</i>
            txt3 = td[td.find("<b>")+3 : td.find("</b>")] #get 3rd data from <b>...</b>
            print txt1
            print txt2
            print txt3
    
    #抓取所有td
    所有文本=soup.findAll(“td”)
    用于所有_文本中的txt
    #将td转换为字符串
    td=str(td)
    txt1=td[td.find(“>”)+1:td.find(“”)#从。。。
    txt2=td[td.find(“”+3:td.find(“”)]#从。。。
    txt3=td[td.find(“”+3:td.find(“”)]#从。。。
    打印txt1
    打印txt2
    打印txt3
    
    这只是简单的字符串操作,并非真正的BS4问题。可以做如下的事情。请注意,下面的方法可能不是最好的方法,但我这样做是为了避免冗长

    from bs4 import BeautifulSoup as bsoup
    
    ofile = open("test.html")
    soup = bsoup(ofile)
    soup.prettify()
    
    tds = soup.find_all("td")
    templist = [td.get_text() for td in tds]
    
    newlist = []
    for temp in templist:
        whole = temp.split(":") # Separate by ":" first.
        half = whole[0].split("(") # Split the first half using open parens.
        first = half[0].strip() # First of three elements.
        second = half[1].replace(")","").strip() # Second of three elements.
        third = whole[1].strip() # Use the second element for the first split to get third of three elements.
        newlist.append([first, second, third])
    
    for lst in newlist:
        print lst # Just print it out.
    
    结果:

    [u'Company No.', u'CO.', u'056']
    [u'Country Code.', u'CC.', u'3532']
    [u'Register', u'Reg.', u'FD522']
    [u'Credit', u'CD.', u'YES']
    [u'Type', u'TP.', u'PRIVATE']
    [Finished in 1.1s]
    

    让我们知道这是否有帮助。

    了解您的尝试是如何失败的,以及返回的结果将非常有用。:)谢谢塔瓦林。我已经在问题中提供了。谢谢philshem。如何获取“:”之前和之后的文本?在您的情况下,print line.split()[0]提供“:”之前的内容。有更强大的方法可以做到这一点,但请让我知道这是否适用于您的应用程序。感谢Nanashi的回复。当我运行它时,错误消息是“IndexError:list index out-range”,问题是“half[1]”。当它们被更改为“半[0]”时,它会起作用。无论如何,我需要消化它们。再次感谢。谢谢zlogic,您的演示方式提供了一个很好的思路。
    for line in soup.get_text().split('\n'):
        if line != ''
            print line.split()
    
    <TD width="40%">Company No. <I>(CO.)</I> : <B>056</B></TD>
    
    #grab all td
    all_texts = soup.findAll("td") 
    for txt in all_texts
            #convert td into string
            td = str(td)
            txt1 = td[td.find(">")+1 : td.find("<i>")] #get first data from <td>...</i>
            txt2 = td[td.find("<i>")+3 : td.find("</i>")] #get 2nd data from <i>...</i>
            txt3 = td[td.find("<b>")+3 : td.find("</b>")] #get 3rd data from <b>...</b>
            print txt1
            print txt2
            print txt3
    
    from bs4 import BeautifulSoup as bsoup
    
    ofile = open("test.html")
    soup = bsoup(ofile)
    soup.prettify()
    
    tds = soup.find_all("td")
    templist = [td.get_text() for td in tds]
    
    newlist = []
    for temp in templist:
        whole = temp.split(":") # Separate by ":" first.
        half = whole[0].split("(") # Split the first half using open parens.
        first = half[0].strip() # First of three elements.
        second = half[1].replace(")","").strip() # Second of three elements.
        third = whole[1].strip() # Use the second element for the first split to get third of three elements.
        newlist.append([first, second, third])
    
    for lst in newlist:
        print lst # Just print it out.
    
    [u'Company No.', u'CO.', u'056']
    [u'Country Code.', u'CC.', u'3532']
    [u'Register', u'Reg.', u'FD522']
    [u'Credit', u'CD.', u'YES']
    [u'Type', u'TP.', u'PRIVATE']
    [Finished in 1.1s]