获取 table 内的 href

Get href within a table

抱歉,之前很可能有人问过我,但我似乎无法在 stack/from 搜索引擎上找到答案。

我正在尝试从 table 中抓取一些数据,但我需要获取 href link。 Html如下:

<table class="featprop results">
<tr>
**1)**<td class="propname" colspan="2"><a href="/lettings-search-results?task=View&amp;itemid=136" rel="nofollow"> West Drayton</a></td>
</tr>
<tr><td class="propimg" colspan="2">

    <div class="imgcrop">
    **2)**<a href="/lettings-search-results?task=View&amp;itemid=136" rel="nofollow"><img src="content/images/1/1/641/w296/858.jpg" alt=" Ashford" width="148"/></a>


    <div class="let">&nbsp;</div>
    </div>
</td></tr>

<tr><td class="proprooms">

到目前为止,我使用了以下内容:

for table in soup.findAll('table', {'class': 'featprop results'}):
    for tr in table.findAll('tr'):
        for a in tr.findAll('a'):
            print(a)

returns 上面的 1 和 2 html,谁能帮我去掉 href link?

for table in soup.findAll('table', {'class': 'featprop results'}):
    for tr in table.findAll('tr'):
        for a in tr.findAll('a'):
            print(a['href'])

输出:

/lettings-search-results?task=View&itemid=136
/lettings-search-results?task=View&itemid=136

Attributes

编辑:

links = set() # set will remove the dupilcate
for a in tr.findAll('a', href=re.compile(r'^/lettings-search-results?')):
    links.add(a['href'])

regular expression

这会在所选 class 名称的元素下为您提供一组标签。

result = soup.select(".featprop a");
for a in result:
    print(a['href']) 

给你以下结果:

/lettings-search-results?task=View&itemid=136
/lettings-search-results?task=View&itemid=136