首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用优美汤从td元素中提取URL

使用优美汤从td元素中提取URL
EN

Stack Overflow用户
提问于 2016-11-07 20:22:48
回答 1查看 1.6K关注 0票数 1

我正在尝试从html表中提取URL。URL位于td单元格内的锚标记中。html看起来像:

代码语言:javascript
复制
<table width="100%" border="0" cellspacing="0" cellpadding="0" name="TabName" id="Tab" class="common-table">
    <tr>
        <td>Acme Company</a><br/><span class="f-10">07-11-2016</span></td>
        <td><span>Vendor</span><br>
        <td><a href="http://URL" title="Report Details">Details</a></td>
    </tr>
</table>

下面是我编写的Python代码:

代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests
import re

r = requests.get('http://SourceURL')
soup = BeautifulSoup(r.content,"html.parser")
# Find table
table = soup.find("table",{"class": "common-table"})
# Find all tr rows
tr = table.find_all("tr")

for each_tr in tr:
    td = each_tr.find_all('td')
    # In each tr rown find each td cell
    for each_td in td:
        print(each_td.text)
        if(each_td.text == "Details"):

我一直遍历到具有URL的最终td标记。现在如何提取URL?

提前谢谢你的时间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-07 20:25:11

就像这样:

url = each_td.a['href']

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40473919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档