简体   繁体   中英

Get the 'href' tag after text using BeautifulSoup in python

What i want to get is the 'href' with the corresponding text whenever i search for the word which has href link. In this example if i search for the word 'over' from the 'div' below, i need it to display "over + 'href' ".

Sample of the html i used :
html '''
<div class="ez" style="" data-ft="&#123;&quot;tn&quot;:&quot;*s&quot;&#125;"> 
<span><p>This is the text here</p> <a href=" my link 3 ">More</a>
<div class="bl" style="" data-ft="&#123;&quot;tn&quot;:&quot;*s&quot;&#125;">
<span><p>Hello everybody over there</p><a href="my link 1></div><div 
class="ol"...><div class="bq qr"><a> class "gh" href="my link 2"</a>
'''html

enter code here 
    for text_href in soup.findAll('div'):
        word = text_href.text
        link = text_href['href']
        print(word '+' link)
for list in word:
    pattern =re.compile(r'over', re.I|re.UNICODE)
    matches = pattern.finditer(c)
        for match in matches:
            print(match) + print(link)

So the out put i was expecting is to flag out the match which is 'over' in my case the and the link(href) which the match 'over' located. result: over + 'the link i want to obtain'(which is the href)

I think you are looking for something like this:

for text_href in soup.findAll('div'):
    word = text_href.text
    if 'over' in word:
        print(text_href.a['href'])

Output:

 the link i want to obtain 

You can use the find_next method if the link is always going to appear after the search text.

Something like this -

html_doc ='''
<div class="ez" style="" data-ft="&#123;&quot;tn&quot;:&quot;*s&quot;&#125;"> 
<span><p>This is the text over here</p> <a href="the link i want to obtain 
">More</a>
<div class="bl" style="" data-ft="&#123;&quot;tn&quot;:&quot;*s&quot;&#125;">
<span><p>Hello everybody</p> <a href="www.mylink...">More</a>
'''

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup(html_doc, 'html.parser')

search_string = 'over'

print(search_string, '+', soup.find(string=re.compile(search_string, re.I)).find_next('a')['href']) # over + the link i want to obtain

You can update the regex accordingly if you are looking for whole words.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM