我有以下的HTML重复数次的网页上(请不要判断):的Python:从HTML中提取分隔文本与BeautifulSoup
<div class="container">
<div class="image">
<a href="#" title="#" class="#">
<img src="img.jpg" alt="#" class="#">
</a>
</div>
<div class="text">
<a href="#">
<h4 class="h4-class">{TITLE}</h4>
{SOME TEXT 1}<br />
<h5><img src="img.jpg" alt="#" /> {SOME TEXT 2}</h5>
{SOME TEXT 3} </a>
</div>
</div>
我想提取{TITLE}
,{SOME TEXT 1}
,{SOME TEXT 2}
和{SOME TEXT 3}
我的代码如下:
from BeautifulSoup import BeautifulSoup as bs
import urllib2
html = urllib2.urlopen('text')
soup = bs(html)
divs = soup.findAll("div", { "class" : "text" })
for div in divs:
inner_text = div.text
strings = inner_text.split("\n")
print strings[0] ## I want this to print just {TITLE}
上打印出来,它打印一条线连接所有的t他认为例如
{TITLE}{SOME TEXT 1}{SOME TEXT 2}{SOME TEXT 3}
反正是有解决这个?我错过了什么?
我不认为在给定的HTML代码的任何'list_txt'。 – arsho
在此处添加它时发生错字。问题依然存在。 – pee2pee