如何使用BeautifulSoup解析文本

时间:2015-09-03 09:30:23

标签: python beautifulsoup

目前,我尝试解析HTML文本,以便只保存2或3个元素。 我的代码看起来像这样:

#!/usr/bin/env python
# coding: utf8

from bs4 import BeautifulSoup

html_doc = """
<div class="postcodedata">
   <b>Latitude:</b> 51.19 degrees<br>
   <b>Longitude:</b> 0.07 degrees<br>
   <b>Postcode Town:</b> Tonbridge<br>
   <b>Easting:</b> 545102 degrees<br>
   <b>Northing:</b> 145533 degrees<br>
   <b>Grid Ref:</b> TQ451455<br>
   <b>District:</b> Sevenoaks<br>
   <b>Ward:</b> Edenbridge South and West<br>
   <b>Satnav:</b> TN8<br>
   <b><a href="phonecodes/"><u>STD Phone Code</u></a>:</b>
   (01959) xxxxxx
   <div class="clear"></div>
</div>
"""

soup = BeautifulSoup(html_doc,'html.parser')

for hit in soup.findAll(attrs={'class' : 'postcodedata'}):
    print hit.text

我想提取&#34; Postcode Town&#34;,&#34; satnav&#34;以及&#34; STD电话代码&#34;。

我如何管理这种提取?

2 个答案:

答案 0 :(得分:2)

简单方法,您只需添加管理数据的方式:

from bs4 import BeautifulSoup

html_doc = """
<div class="postcodedata">
   <b>Latitude:</b> 51.19 degrees<br>
   <b>Longitude:</b> 0.07 degrees<br>
   <b>Postcode Town:</b> Tonbridge<br>
   <b>Easting:</b> 545102 degrees<br>
   <b>Northing:</b> 145533 degrees<br>
   <b>Grid Ref:</b> TQ451455<br>
   <b>District:</b> Sevenoaks<br>
   <b>Ward:</b> Edenbridge South and West<br>
   <b>Satnav:</b> TN8<br>
   <b><a href="phonecodes/"><u>STD Phone Code</u></a>:</b>
   (01959) xxxxxx
   <div class="clear"></div>
</div>
"""

soup = BeautifulSoup(html_doc,'html.parser')

data = soup.find(attrs={'class' : 'postcodedata'})
#split line by line
values = data.text.split('\n');

for i in range(len(values)):
    #for each line split by semicolon so line[0] has the key and line[1] has the value
    line = values[i].split(':')
    #check the required key 
    if line[0]=='Postcode Town' or line[0]=='Satnav' or line[0] =='STD Phone Code':
         print line[1]

希望帮助你!

答案 1 :(得分:0)

我找到了一个解决方案:

#!/usr/bin/env python
# coding: utf8

from bs4 import BeautifulSoup

html_doc = """
<div class="postcodedata">
   <b>Latitude:</b> 51.19 degrees<br>
   <b>Longitude:</b> 0.07 degrees<br>
   <b>Postcode Town:</b> Tonbridge<br>
   <b>Easting:</b> 545102 degrees<br>
   <b>Northing:</b> 145533 degrees<br>
   <b>Grid Ref:</b> TQ451455<br>
   <b>District:</b> Sevenoaks<br>
   <b>Ward:</b> Edenbridge South and West<br>
   <b>Satnav:</b> TN8<br>
   <b><a href="phonecodes/"><u>STD Phone Code</u></a>:</b>
   (01959) xxxxxx
   <div class="clear"></div>
</div>
"""

soup = BeautifulSoup(html_doc,'html.parser')
data = ""
for hit in soup.findAll(attrs={'class' : 'postcodedata'}):
    data = hit.text.strip()

rest = str(data)
print rest
print "*************************"
count = 1 
for line in rest.splitlines():
    if count == 3:
        town = (line).replace("Postcode Town:", "").strip()
        print town 
    if count == 9:
        satnav = (line).replace("Satnav:", "").strip()
        print satnav
    if count == 11:
        phonecodes = (line).strip()
        print phonecodes
    count += 1