-1

以下html代码:

<div class="rating-list">
<ul class="recommend">
<li>
<span class="recommend-titleInline">Stayed April 2013, traveled as a couple</span>
<ul class="recommend-column first">
<li class="recommend-answer">
<span class="rate rate_ss ss50">
<img class="sprite-ratings" src="http://c1.tacdn.com/img2/x.gif" alt="5 of 5 stars" content="5.0"/>
</span>
Value</li>
<li class="recommend-answer">
<span class="rate rate_ss ss50">
<img class="sprite-ratings" src="http://c1.tacdn.com/img2/x.gif" alt="5 of 5 stars" content="5.0"/>
</span>
Location</li>
<li class="recommend-answer">
<span class="rate rate_ss ss50">
<img class="sprite-ratings" src="http://c1.tacdn.com/img2/x.gif" alt="5 of 5 stars" content="5.0"/>
</span>
Sleep Quality</li>
</ul>
<ul class="recommend-column">
<li class="recommend-answer">
<span class="rate rate_ss ss50">
<img class="sprite-ratings" src="http://c1.tacdn.com/img2/x.gif" alt="5 of 5 stars" content="5.0"/>
</span>
Rooms</li>
<li class="recommend-answer">
<span class="rate rate_ss ss50">
<img class="sprite-ratings" src="http://c1.tacdn.com/img2/x.gif" alt="5 of 5 stars" content="5.0"/>
</span>
Cleanliness</li>
<li class="recommend-answer">
<span class="rate rate_ss ss50">
<img class="sprite-ratings" src="http://c1.tacdn.com/img2/x.gif" alt="5 of 5 stars" content="5.0"/>
</span>
Service</li>
</ul>
</li>
</ul>
</div>

现在我已经使用 Beautifulsoup 获取整个标签,然后我想像这样获取“li”标签:</p>

valueRatingTag = subRatingListTags[i].find(name = 'li', attrs = { 'class' : 'recommend-answer' }, text = 'Value')
locationRatingTag = subRatingListTags[i].find(name = 'li', attrs = { 'class' : 'recommend-answer' }, text = 'Location')
sleepRatingTag = subRatingListTags[i].find(name = 'li', attrs = { 'class' : 'recommend-answer' }, text = 'Sleep Quality')
        roomRatingTag = subRatingListTags[i].find(name = 'li', attrs = { 'class' : 'recommend-answer' }, text = 'Rooms')
        cleanRatingTag = subRatingListTags[i].find(name = 'li', attrs = { 'class' : 'recommend-answer' }, text = 'Cleanliness')
        serviceRatingTag = subRatingListTags[i].find(name = 'li', attrs = { 'class' : 'recommend-answer' }, text = 'Service')

但似乎失败了。这六个变量都是无,这不是我所期望的。我该怎么办

4

2 回答 2

0

你不清楚你想要什么。反正:

>>> lis = [t for t in soup.find_all('li', 'recommend-answer')]
>>> lis[0].text
'\n\n\n\nValue'
>>> lis[1].text
'\n\n\n\nLocation'
>>> lis[0].img['alt']
'5 of 5 stars'

您肯定希望在开始解析之前对 html 进行预处理以删除所有换行符。

于 2013-05-08T08:02:10.123 回答
0

将使用正则表达式作为参数来text提供帮助吗?

subRatingListTags[i].find(text=re.compile("Location"))

换行符可能会导致此处的精确文本匹配失败。

于 2013-05-08T06:38:24.913 回答