网页源代码包含如下片段:
<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>
简称,如公司,抄送。规则。CD。和TP。是斜体字体。056、3532、FD522等内容均为粗体。它们之间用“:”隔开。在
我想用beauthulsoup分别提取它们的标题和内容,但是没有成功。在
我正在使用:
^{pr2}$但效果不好。它在一行中返回“Company No.(CO.):056”,但我希望它们分开,比如“Company No.”、“CO.”和“056”。在
我也试过:
all_texts = soup.find_all(":")
或者:
all_texts = soup.find_all("/b")
等等,但它们不起作用。在
下面的帮助有两种方法。放在那里作为参考:
这样可以获得粗体字母的内容,但是在某些句子中,最后一个字母会丢失:
for bb in aa:
cc = bb.get_text()
dd = cc[cc.find("<b>")+1 : cc.find("</b>")]
print dd
这样,ee和ff提供了“标题”和内容,即“:”前后的文本。在
for bb in aa:
cc = bb.get_text()
dd = cc.split(' :')
ee = dd[0] #title
ff = dd[len(dd)-1] # content
你不必强迫自己使用beauthulsoup函数来分离它们 因为对于每个数据里面都有不同的令牌密钥来分割 即:
<B></B>
我建议您使用子字符串方法从每个td中获取数据:
^{pr2}$这只是简单的字符串操作,并不是真正的BS4问题。可以做如下的事情。请注意,下面的方法可能不是最好的方法,但我这样做是为了冗长。在
结果:
^{pr2}$让我们知道这是否有帮助。在
使用findAll获取完整HTML文档的正确部分,然后使用:
然后用“.split()”将其拆分为数组
^{pr2}$相关问题 更多 >
编程相关推荐