这是html代码的一部分,表示我想从网页中提取的信息。我的目的是只提取b标记之间的名称和值。我期望的结果是一个类似这样的列表:[On,DVI,396,2035,2551]
...
<div class="txt"><br>
Power: <b>On</b><br><br>
Source: <b>DVI</b><br><br>
Lamp runtime: <b>396</b> hours<br>
Lamp remaining: <b>2035</b> hours<br>
Total operation: <b>2551</b> hours<br>
</div>
...
我尝试的是:
from bs4 import BeautifulSoup
import urllib2
url='ip address here'
html=urllib2.urlopen(url).read()
soup=BeautifulSoup(html)
main_div=soup.find("div",{"class":"txt"})
data=main_div.findAll('b').text
出了什么问题?仅供参考,我是初学者,所以请原谅我。
try
data=[b.string for b in main_div.findAll('b')]
也许是这样的?
import BeautifulSoup
html = '''<div class="txt"><br>
nPower: <b>On</b><br><br>
nSource: <b>DVI</b><br><br>
nLamp runtime: <b>396</b> hours<br>
nLamp remaining: <b>2035</b> hours<br>
nTotal operation: <b>2551</b> hours<br>
n</div>'''
soup = BeautifulSoup.BeautifulSoup(html)
bTags = []
for i in soup.findAll('b'):
bTags.append(i.text)
bTags目录:
[u'On', u'DVI', u'396', u'2035', u'2551']