2016-12-17 7 views
0

私はスクレイピングを初めてやっています。私はテーブルでフォームをかき集めようとしています。私は美しいスープで親タグ全体をこすり落とすことができます。しかし、私は子供のタグを通過し、それらの中にテキストを取得する方法がわかりません。ここBeautifulSoup:テーブルを解析するときに名前エラーが発生する

は、上記のコードは<td>タグ内のすべてのものを印刷し、私のコード

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---* 

です。エラーは、子タグをたどろうとすると発生します。ここで

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' 

は、私は内部の "オンラインresgistrationフォーム" のテキストを取得したい私の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> 

です。これについてどうすればいいですか?

答えて

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 
関連する問題