2016-12-17 59 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