使用 BeautifulSoup 提取锚标记值

发布于 2024-11-25 13:09:28 字数 879 浏览 0 评论 0原文

我正在尝试使用 BeautifulSoup 从网站中提取值。这些值本质上是搜索结果,在本例中是特定地区的药房。我试图从中提取的页面源包含以下 HTML:

<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_1" class="sr-item-link" href="http://www.mocality.co.ke/b/applegene-pharmacy/applegene/brooklyn/health-and-beauty-medical/_/airtime-chemist-cosmetics-medicine/d42f7388-3f9b-4a34-8971-dc6ae9692586?skw=pharmacys&amp;rcnt=10">Applegene Pharmacy</a>

锚标记的 id 根据结果递增,因此下一个标记为 2:

<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_2" class="sr-item-link" href="http://www.mocality.co.ke/b/natros-pharmacy/natrosoh/innercore/medical-services/_/_/0cfe6a11-7bee-41f8-8d2e-6a472557201f?skw=pharmacys&amp;rcnt=10">Natros Pharmacy</a>

我使用了 findAll('a') 但这给出了我所有的锚标签。我如何使用 BeautifulSoup 来解析它并提取特定锚标签的值?

I'm trying to use BeautifulSoup to extract values from a site. The values are essentially search results, in this case pharmacies in a particular region. The source of the page I'm trying to extract from contains the following HTML:

<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_1" class="sr-item-link" href="http://www.mocality.co.ke/b/applegene-pharmacy/applegene/brooklyn/health-and-beauty-medical/_/airtime-chemist-cosmetics-medicine/d42f7388-3f9b-4a34-8971-dc6ae9692586?skw=pharmacys&rcnt=10">Applegene Pharmacy</a>

the id of the anchor tag is incremented based on the results so the next one has a 2:

<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_2" class="sr-item-link" href="http://www.mocality.co.ke/b/natros-pharmacy/natrosoh/innercore/medical-services/_/_/0cfe6a11-7bee-41f8-8d2e-6a472557201f?skw=pharmacys&rcnt=10">Natros Pharmacy</a>

I've used findAll('a') but that gives me all anchor tags.How can I use BeautifulSoup to parse this and extract the values of the specific anchor tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍暮颜 2024-12-02 13:09:28
from BeautifulSoup import BeautifulSoup

txt = '''<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_1" class="sr-item-link" href="http://www.mocality.co.ke/b/natros-pharmacy/natrosoh/innercore/medical-services/_/_/0cfe6a11-7bee-41f8-8d2e-6a472557201f?skw=pharmacys&rcnt=10">Natros Pharmacy</a>
<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_2" class="sr-item-link
" href="http://www.mocality.co.ke/b/natros-pharmacy/natrosoh/innercore/medical-services/_/_/0cfe6a11-
7bee-41f8-8d2e-6a472557201f?skw=pharmacys&rcnt=10">Natros Pharmacy</a>'''
match = 'body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile'

soup = BeautifulSoup(txt)
for a in soup.findAll('a'):
        if a.has_key('id') and a['id'].startswith(match):
               print a['href'], a.contents
from BeautifulSoup import BeautifulSoup

txt = '''<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_1" class="sr-item-link" href="http://www.mocality.co.ke/b/natros-pharmacy/natrosoh/innercore/medical-services/_/_/0cfe6a11-7bee-41f8-8d2e-6a472557201f?skw=pharmacys&rcnt=10">Natros Pharmacy</a>
<a id="body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile_2" class="sr-item-link
" href="http://www.mocality.co.ke/b/natros-pharmacy/natrosoh/innercore/medical-services/_/_/0cfe6a11-
7bee-41f8-8d2e-6a472557201f?skw=pharmacys&rcnt=10">Natros Pharmacy</a>'''
match = 'body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile'

soup = BeautifulSoup(txt)
for a in soup.findAll('a'):
        if a.has_key('id') and a['id'].startswith(match):
               print a['href'], a.contents
半透明的墙 2024-12-02 13:09:28

使用 find 的关键字参数来限制属性:

find("a", id="whatever_1")

您还可以使用(布尔)函数调用 find

def isRight(tag):
    return ...

findAll(isRight)

Use the keyword arguments of find, which restrict the attributes:

find("a", id="whatever_1")

You can also call find with a (boolean) function:

def isRight(tag):
    return ...

findAll(isRight)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文