Html 添加特定关键字的链接

Html 添加特定关键字的链接,html,beautifulsoup,Html,Beautifulsoup,我正在尝试为某些关键字添加内部链接,比如将所有单词索引链接到我网站的主页 我计划使用BeautifulSoup4,但我不知道如何为元素中的某个单词添加标记 这就是我想要的 <div>You can find the content from the index page</div> 我更喜欢在Beautifulsoup中实现,逻辑包括: 检查word是否已在链接中 如何提取特定单词并添加链接 下面我浏览了所有的div标签,看看是否有指向index.html的链接,如果没

我正在尝试为某些关键字添加内部链接,比如将所有单词
索引
链接到我网站的主页

我计划使用BeautifulSoup4,但我不知道如何为元素中的某个单词添加标记

这就是我想要的

<div>You can find the content from the index page</div>
我更喜欢在Beautifulsoup中实现,逻辑包括:

  • 检查word是否已在链接中
  • 如何提取特定单词并添加链接
    下面我浏览了所有的
    div
    标签,看看是否有指向index.html的链接,如果没有,我检查是否有“index”这个词。如果是,我创建一个新的
    div
    ,添加文本的第一部分,使用index.html链接添加一个新的
    a
    标记,然后添加文本的其余部分,否则它只会通过

    soup = BeautifulSoup("<div>You can find content from the current index page.</div><div>You can find content from the <a href='index.html'>index</a> page.</div><div>Just random text</div>")
    print(soup)
    div_data = soup.find_all("div")
    newsoup = BeautifulSoup("<h1></h1>")
    i = 1
    for item in div_data:
        if item.find("a", {"href":"index.html"}):
            newitem = item
        elif item.text.find("index") > -1:
            newitem = newsoup.new_tag("div")
            indexItem = newsoup.new_tag("a", href="index.html")
            indexItem.string="index"
            newitem.string = item.text.split("index")[0]
            newitem.insert(1,indexItem)
            newitem.insert(2,item.text.split("index")[1])
        else:
            newitem = item
        newsoup.body.insert(i,newitem)
        i += 1
    
    print(newsoup)
    
    soup=beautifulsou(“您可以从当前索引页面中找到内容。您可以从页面中找到内容。只需随机文本”)
    印花(汤)
    div_data=soup.find_all(“div”)
    新闻组=美化组(“”)
    i=1
    对于div_数据中的项目:
    如果item.find(“a”,{“href”:“index.html”}):
    newitem=项目
    elif item.text.find(“索引”)>-1:
    newitem=newsoup.new_标记(“div”)
    indexItem=newsoup.new_标记(“a”,href=“index.html”)
    index.string=“index”
    newitem.string=item.text.split(“索引”)[0]
    newitem.insert(1,indexItem)
    newitem.insert(2,item.text.split(“索引”)[1])
    其他:
    newitem=项目
    newsoup.body.insert(i,newitem)
    i+=1
    印刷品(新闻组)
    
    输出为:

    <html><body><div>You can find content from the current index page.</div>
    <div>You can find content from the <a href="index.html">index</a> page.</div>
    <div>Just random text</div></body></html>
    
    <html><body><h1></h1><div>You can find content from the current <a href="index.html">index</a> page.</div>
    <div>You can find content from the <a href="index.html">index</a> page.</div>
    <div>Just random text</div></body></html>
    
    您可以从当前索引页中找到内容。
    您可以从页面中找到内容。
    只是随机的文本
    您可以从当前页面中找到内容。
    您可以从页面中找到内容。
    只是随机的文本
    
    您可以通过检查单词“index”是否多次出现来导入它,但这将帮助您开始

    soup = BeautifulSoup("<div>You can find content from the current index page.</div><div>You can find content from the <a href='index.html'>index</a> page.</div><div>Just random text</div>")
    print(soup)
    div_data = soup.find_all("div")
    newsoup = BeautifulSoup("<h1></h1>")
    i = 1
    for item in div_data:
        if item.find("a", {"href":"index.html"}):
            newitem = item
        elif item.text.find("index") > -1:
            newitem = newsoup.new_tag("div")
            indexItem = newsoup.new_tag("a", href="index.html")
            indexItem.string="index"
            newitem.string = item.text.split("index")[0]
            newitem.insert(1,indexItem)
            newitem.insert(2,item.text.split("index")[1])
        else:
            newitem = item
        newsoup.body.insert(i,newitem)
        i += 1
    
    print(newsoup)
    
    <html><body><div>You can find content from the current index page.</div>
    <div>You can find content from the <a href="index.html">index</a> page.</div>
    <div>Just random text</div></body></html>
    
    <html><body><h1></h1><div>You can find content from the current <a href="index.html">index</a> page.</div>
    <div>You can find content from the <a href="index.html">index</a> page.</div>
    <div>Just random text</div></body></html>