2016-12-17 57 views
0

我是新來抓取的。我正在試圖用表格刮一個表格。我可以用美麗的湯刮整個父母的標籤。但我不確定如何遍歷兒童標籤並獲取其中的文字。BeautifulSoup:通過表解析時發生名稱錯誤

這裏是我的代碼

soup = BeautifulSoup(htmltext, "html.parser") 
tables = soup.find('td',attrs={'class':'title_heading'}) 
for table in tables: 
    print(table) 
    form_name = table.td.center.strong.u.text *--ERROR---* 

上面的代碼打印<td>標籤內的所有內容。當我嘗試遍歷子標記時發生錯誤。

File "E:\Study_naveen\python\scrape.py", line 23, in <module> 
form_name = table.td.center.strong.u.text 
AttributeError: 'NoneType' object has no attribute 'center' 

這裏是我的html

<td width="615" class="title_heading"><center> 
<strong><u> ONLINE REGISTRATION FORM</u></strong> 
<br><br> 
<strong>Blah<br> 
123456789-<br> 
blah blah<br> 
phone - 123456789 
999999999<br> 
Email : [email protected]</strong> 

我想裏面的 「在線resgistration形式」 文本。我如何去做這件事?

回答

0
html = '''<td width="615" class="title_heading"><center> 
<strong><u> ONLINE REGISTRATION FORM</u></strong> 
<br><br> 
<strong>Blah<br> 
123456789-<br> 
blah blah<br> 
phone - 123456789 
999999999<br> 
Email : [email protected]</strong>''' 
import bs4 

soup = bs4.BeautifulSoup(html, 'lxml') 
text = soup.find('td', class_="title_heading").find('strong').text 
print(text) 

出來:

ONLINE REGISTRATION FORM