问题:如何剥离此链接以删除不需要的数据(bs4)?

这是 HTML 的样子:

<div class="full-news none">
     Demo: <a href="https://www.lolinez.com/?https://www.makemytrip.com" 
    rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a>
   <br/>

怎样才能把这部分从href:https://www.lolinez.com/?中去掉,让最终的输出变成这样:

 <div class="full-news none">
         Demo: <a href="https://www.makemytrip.com" 
        rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a>
       <br/>

我试过用美汤的decompose功能,但是完全去掉了整个标签,请问怎么解决?

解答

注意 如果没有额外的上下文,我会缩小到以下方法

选项#1

将您的子字符串替换为您传递给BeautifulSoup构造函数的string:

soup = BeautifulSoup(YOUR_STRING.replace('https://www.lolinez.com/?',''), 'lxml')
选项#2

替换soup中的子字符串,您可以选择所有包含www.lolinez.com<a>并替换其href的值:

for x in soup.select('a[href*="www.lolinez.com"]'):
    x['href'] = x['href'].replace('https://www.lolinez.com/?','')
示例
import bs4, requests
from bs4 import BeautifulSoup

html='''
<a href="https://www.lolinez.com/?https://www.makemytrip.com" rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a>
<a href="https://www.makemytrip.com" rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a>
<a href="https://www.lolinez.com/?https://www.makemytrip.com" rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a>
'''

soup = BeautifulSoup(html, 'lxml')

for x in soup.select('a[href*="www.lolinez.com"]'):
    x['href'] = x['href'].replace('https://www.lolinez.com/?','')
    
soup
输出
<html><body><a href="https://www.makemytrip.com" rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a><a href="https://www.makemytrip.com" rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a><a href="https://www.makemytrip.com" rel="external noopener noreferrer" target="_blank">https://www.makemytrip.com</a></body></html>
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐