python - 用漂亮的汤 4 python 抓取网页

标签 python html beautifulsoup

所以我刚开始使用 beautiful soup 4,我遇到了一个问题,我已经尝试解决了几天,但我不能。 让我先贴上我要分析的html代码:

<table class="table table-condensed table-hover tenlaces tablesorter">
<thead>
<tr>
<th class="al">Language</th>
<th class="ac">Link</th>
</tr>
</thead>
<tbody>


            <tr>
            <td class="tdidioma"><span class="flag flag_0">0</span></td>
            <td class="tdenlace"><a class="btn btn-mini enlace_link" data-servidor="42" rel="nofollow" target="_blank" title="Ver..." href="LINK I WANT TO SAVE0"><i class="icon-play"></i>&nbsp;&nbsp;Ver</a></td>
            </tr>

            <tr>
            <td class="tdidioma"><span class="flag flag_1">1</span></td>
            <td class="tdenlace"><a class="btn btn-mini enlace_link" data-servidor="42" rel="nofollow" target="_blank" title="Ver..." href="LINK I WANT TO SAVE1"><i class="icon-play"></i>&nbsp;&nbsp;Ver</a></td>
            </tr>

            <tr>
            <td class="tdidioma"><span class="flag flag_2">2</span></td>
            <td class="tdenlace"><a class="btn btn-mini enlace_link" data-servidor="42" rel="nofollow" target="_blank" title="Ver..." href="LINK I WANT TO SAVE2"><i class="icon-play"></i>&nbsp;&nbsp;Ver</a></td>
            </tr>
</tbody>
</table>

如您所见,在每个 中都有 语言和链接。问题是我不知道如何将语言与链接联系起来。我的意思是,我想选择例如,如果语言中的空格是 1,则返回链接。如果没有,请不要做任何事情。但我只能用语言返回 < td > ,而不是所有 < tr > 这是重要的想法 我不知道我是否表达了我的观点,因为我真的不知道如何解释

我现在拥有的代码从我的主 url 获取 < tbody >,但我真的不知道如何做到这一点。

谢谢,抱歉我的英语不好!

编辑: 这是我的代码示例,因此您可以看到我正在使用的库以及所有内容

from bs4 import BeautifulSoup
import urllib2

url = raw_input("Introduce URL to analyse: ")
page = urllib2.urlopen(url)
soup = Beautifulsoup(page.read())
body = soup.tbody
#HERE SHOULD BE WHAT I DON'T KNOW HOW TO DO
page.close()

最佳答案

尝试这样的事情:

result = None
for row in soup.tbody.find_all('tr'):
    lang, link = row.find_all('td')
    if lang.string == '1':
        result = link.a['href']
print result

关于python - 用漂亮的汤 4 python 抓取网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24451747/

相关文章:

python - 在 seaborn facetgrid 的各个方面设置轴限制

javascript - 如何使用 Knockout.js 绑定(bind) HTML5 日期选择器和其他输入类型?

Beautiful Soup 的 Python 正则表达式

python - Beautiful Soup Find() 返回 AttributeError

python - 成功登录时 Paramiko 返回代码

Python Lambdas 和变量绑定(bind)

python - Tabula-py 没有正确拆分列

android - 在 NativeScript for Android 中使用边框

javascript - jQuery - 复选框正在计数,但单击全选复选框时不计数

python - BeautifulSoup : Get the Contents of Sub-Nodes