2016-05-23 11 views
0

私はpython/BeautifulSoup初心者です。<td width="473" valign="top"> - ><strong>のすべてのコンテンツを抽出しようとしています。python beautifulsoup再帰解析

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl"> 
<head> 
    <title>MIEJSKI OŚRODEK KULTURY W ŻORACH Repertuar Kina Na Starówce</title> 
</head> 
<body> 
<div class="page_content"> 
<p>&nbsp;</p> 
<p> 
<table style="width: 450px;" border="1" cellspacing="0" cellpadding="0"> 
<tbody> 
<tr> 
<td width="57" valign="top"> 
<p align="center"><strong>Data</strong></p> 
</td> 
<td width="473" valign="top"> 
<p align="center"><strong>Tytuł Filmu</strong></p> 
</td> 
<td width="95" valign="top"> 
<p align="center"><strong>Godzina</strong></p> 
</td> 
</tr> 
<tr> 
<td width="57" valign="top"> 
<p align="center"><strong>&nbsp;</strong></p> 
</td> 
<td width="473" valign="top"> 
<p align="center"><strong>1 - 5.05</strong></p> 
</td> 
<td width="95" valign="top"> 
<p align="center">&nbsp;</p> 
</td> 
</tr> 
<tr> 
<td width="57" valign="top"> 
<p align="center"><strong>1</strong></p> 
</td> 
<td width="473" valign="top"> 
<p align="center"><strong>KINO POWT&Oacute;REK: ZWIERZOGR&Oacute;D </strong>USA/b.o&nbsp; cena 10 zł</p> 
</td> 
<td width="95" valign="top"> 
<p align="center">16:30</p> 
</td> 
</tr> 

</tbody> 
</table> 
</p> 
</body> 
</html> 

私は行くことができる遠いが、このコードですべてのタグのリストを得ることです:

from bs4 import BeautifulSoup 

soup = BeautifulSoup(open("zory1.html"), "html.parser") 

y = soup.find_all(width="473") 

newy = str(y) 

newsoup = BeautifulSoup(newy ,"html.parser") 
stronglist = newsoup.find_all('strong') 

lasty = str(stronglist) 

lastsoup = BeautifulSoup(lasty , "html.parser") 

lst = soup.find_all('strong') 

for item in lst: 
    print item 

私は初心者のレベルでは、タグ内のコンテンツを取り出すことができる方法は?

おかげ

+0

( "td [width = 473]") 'を使用して特定のtdsのみを検索します。なぜselectは複数の属性をサポートしていないのですか? – kpie

答えて

1

は、ノードのテキストを取得するためにget_text()を使用してください。

我々は、すべての行とテーブル内のすべてのセルの上に行く完全な作業例:

from bs4 import BeautifulSoup 

data = """your HTML here""" 
soup = BeautifulSoup(data, "html.parser") 

for row in soup.find_all("tr"): 
    print([cell.get_text(strip=True) for cell in row.find_all("td")]) 

プリント:ここ

['Data', 'Tytuł Filmu', 'Godzina'] 
['', '1 - 5.05', ''] 
['1', 'KINO POWTÓREK: ZWIERZOGRÓDUSA/b.o\xa0 cena 10 zł', '16:30'] 
+1

... lxml.cssSelectは、任意の毛羽立ちなしでこれを行うためにかなり良いです聞い

結果

$ python test.py Tytuł Filmu 1 - 5.05 KINO POWTÓREK: ZWIERZOGRÓD 

0

あなたは

from bs4 import BeautifulSoup 

navigator = BeautifulSoup(open("zory1.html"), "html.parser") 

tds = navigator.find_all("td", {"width":"473"}) 

resultList = [item.strong.get_text() for item in tds] 

for item in resultList: 
    print item 
です 私はsoup.selectにTDのために[td.text.strip() `あなたが望むかもしれない
+0

上品で美しい!ありがとうございました! – Yan

関連する問題