开发者

Using BeautifulSoup to extract anchor tag values

开发者 https://www.devze.com 2023-03-22 06:21 出处:网络
I\'m trying to开发者_JAVA百科 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 tr

I'm trying to开发者_JAVA百科 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&amp;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&amp;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?


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&amp;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&amp;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


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消