简体   繁体   English

在python中使用BeautifulSoup获取文本后的'href'标签

[英]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.每当我搜索具有 href 链接的单词时,我想要得到的是带有相应文本的“href”。 In this example if i search for the word 'over' from the 'div' below, i need it to display "over + 'href' ".在这个例子中,如果我从下面的“div”中搜索“over”这个词,我需要它显示“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.因此,我期望的输出是标记出在我的情况下“结束”的匹配以及匹配“结束”所在的链接(href)。 result: over + 'the link i want to obtain'(which is the href)结果:over + '我想获得的链接'(即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.如果链接总是出现在搜索文本之后,您可以使用find_next方法。

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.如果您要查找整个单词,则可以相应地更新正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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