2012-01-11 114 views
3

我使用BeautifulSoup从这个网站http://www.cpso.on.ca/docsearch/default.aspx解析HTML与Python

这里提取搜索结果的一些数据的HTML代码的样本这一直是.prettify()

<tr> 
<td> 
    <a class="doctor" href="details.aspx?view=1&amp;id= 72374"> 
    Smith, Jane 
    </a> 
    (#72374) 
</td> 
<td> 
    Suite 042 
    <br /> 
    21 Jump St 
    <br /> 
    Toronto&nbsp;ON&nbsp;&nbsp;M4C 5T2 
    <br /> 
    Phone:&nbsp;(555) 555-5555 
    <br /> 
    Fax:&nbsp;(555) 555-555 
</td> 
<td align="center"> 
</td> 
</tr> 

基本上每个<tr>区块有3个<td>区块。

我所要的输出是

Smith, Jane Suite 042 21 Jump St Toronto ON M4C 5T2

我也有一个新行分隔的条目。

我有问题写在第二个<td>块中存储的地址。

我也在写这个文件。

这里是我迄今为止...它不工作:P

for tr in soup.findAll('tr'): 
    #td1 = tr.td 
    td2 = tr.td.nextSibling.nextSibling 

    for a in tr.findAll('a'): 
     target.write(a.string) 
     target.write(" ") 

    for i in range(len(td2.contents)): 
     if i != None: 
      target.write(td2.contents[i].string) 
      target.write(" ") 
    target.write("\n") 
+1

你的第一个'for'环缺少':',和内循环是不缩进。这是实际的代码还是发布错误? – Jacob 2012-01-11 01:57:16

+0

是的。我的错。 Python是我刚刚拿起来做一些快速的HTML解析。我 – KylePDM 2012-01-11 01:59:52

+0

我也想着甚至没有循环td和a,只是当我通过tr循环制作2个温度td值。 – KylePDM 2012-01-11 02:01:30

回答

1

这应该做什么最你想要:

import os 
from BeautifulSoup import BeautifulSoup 

soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES) 

with open('output.txt', 'wb') as stream: 
    for tr in soup.findAll('tr')[1:]: # [1:] skips the header 
     columns = tr.findAll('td') 
     line = [columns[0].a.string.strip()] 
     for item in (item.strip() for item in columns[1].findAll(text=True)): 
      if (item and not item.startswith('Phone:') 
       and not item.startswith('Fax:')): 
       line.append(item) 
     stream.write(' '.join(line).encode('utf-8')) 
     stream.write(os.linesep) 

UPD ATE

增加了一些代码来显示如何写入文件的名称和地址。

还改变了输出,以便姓名和地址写在一行上,电话和传真号码被省略。

+0

当我将打印方法更改为.write时,我得到一个UnicodeEncodeError:'ascii'编解码器无法编码字符u'\ xa0' – KylePDM 2012-01-11 03:06:38

+0

仍然无法使用。 'http://www.crummy.com/software/BeautifulSoup/documentation.html#Why不能美丽的汤打印出我给它的非ASCII字符?' ^说它可能是我的Python安装或与BeautifulSoup本身的东西。据说,这是BeautifulSoup >>; – KylePDM 2012-01-11 03:34:23

+0

@KylePDM。Python或BeautifulSoup都不可能有任何问题。你的代码用于阅读html并编写输出看起来像什么? – ekhumoro 2012-01-11 03:42:01

1
In [243]: soup.getText(' ').replace('&nbsp;', ' ').strip() 
Out[243]: u'Smith, Jane (#72374) Suite 042 21 Jump St Toronto ON M4C 5T2 Phone: (555) 555-5555 Fax: (555) 555-555' 

为了得到你想要什么:

In [246]: address = soup.getText(' ').replace('&nbsp;', ' ').strip() 
In [247]: import re 
In [248]: address = re.sub(r' Phone.*$', '', address) 
In [249]: address = address.replace(' ', ' ') 
In [250]: address = re.sub(r' \(.*?\)', '', address) 
In [251]: print address 
Smith, Jane Suite 042 21 Jump St Toronto ON M4C 5T2 
1

我想尝试这样的事:

from BeautifulSoup import BeautifulSoup 
soup = BeautifulSoup(your_html, 
       convertEntities=BeautifulSoup.HTML_ENTITIES) 

for tr in soup.findAll('tr'): 
    td = tr.findAll('td') 

    target.write(td[0].a.string) 
    target.write(' ') 

    target.write(' '.join(text.strip() for text in td[1].findAll(text = True)[:-2]))) #finds all text subnodes, except 2 last ones (phone number), and joins them with ' ' separator 
    target.write("\n") 
+0

这只是给了我一个换行的名字 – KylePDM 2012-01-11 03:05:37