使用 BeautifulSoup 提取锚标记值
我正在尝试使用 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&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&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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
find
的关键字参数来限制属性:您还可以使用(布尔)函数调用
find
:Use the keyword arguments of
find
, which restrict the attributes:You can also call
find
with a (boolean) function: