Skip to main content
 首页 » 编程设计

python之Beautifulsoup 4 span containg '@' 返回奇怪的结果

2025年05月04日14mfryf

我能够使用以下方法获得所需的跨度列表:

attrs = soup.find_all("span") 

这将返回一个跨度列表作为键和值:

[ 
    <span>back camera resolution</span>,  
    <span class="even">12 MP</span> 
] 
 
[ 
    <span>front camera resolution</span>,  
    <span class="even">16 MP</span> 
] 
 
[ 
    <span>video resolution</span>,  
    <span class="even"><a class="__cf_email__" data-cfemail="b98b888f89c9f98a89dfc9ca" href="/cdn-cgi/l/email-protection">[email protected]</a><script data-cfhash="f9e31" type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script> - <a class="__cf_email__" data-cfemail="4677767e7636067576203635" href="/cdn-cgi/l/email-protection">[email protected]</a><script data-cfhash="f9e31" type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script> - <a class="__cf_email__" data-cfemail="5067626010616260362023" href="/cdn-cgi/l/email-protection">[email protected]</a><script data-cfhash="f9e31" type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script> 
    </span> 
] 

原始的 HTML 是:

为什么'视频分辨率'会这样转换?

请您参考如下方法:

该站点正在使用 CloudFlare email protection feature ,它似乎已将 所有字符串 替换为 @在其中使用混淆(XOR 加密)值,以防止抓取器收集电子邮件地址。每个替换都包含用于对其进行解码的 JavaScript 代码。

BeautifulSoup 不会执行 Javascript,但您的浏览器已经执行了它并替换了 <a class="__cf_email__">带有生成的解密数据的标签。

你可以用一个小的 Python 3 函数来做同样的事情;所有 JavaScript 代码所做的就是通过使用第一个字节作为简单 XOR 解密例程中的 key 来“解密”(十六进制编码的)值:

def decode(cfemail): 
    enc = bytes.fromhex(cfemail) 
    return bytes([c ^ enc[0] for c in enc[1:]]).decode('utf8') 
 
def deobfuscate_cf_email(soup): 
    for encrypted_email in soup.select('a.__cf_email__'): 
        decrypted = decode(encrypted_email['data-cfemail']) 
        # remove the <script> tag from the tree 
        script_tag = encrypted_email.find_next_sibling('script') 
        script_tag.decompose() 
        # replace the <a class="__cf_email__"> tag with the decoded result 
        encrypted_email.replace_with(decrypted) 

要使上述在 Python 2 中工作,请替换 bytes与 bytearray .

演示:

>>> from bs4 import BeautifulSoup 
>>> soup = BeautifulSoup(''' 
...     <span>video resolution</span>, 
...     <span class="even"><a class="__cf_email__" data-cfemail="b98b888f89c9f98a89dfc9ca" href="/cdn-cgi/l/email-protection">[email protected]</a><script data-cfhash="f9e31" type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script> - <a class="__cf_email__" data-cfemail="4677767e7636067576203635" href="/cdn-cgi/l/email-protection">[email protected]</a><script data-cfhash="f9e31" type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script> - <a class="__cf_email__" data-cfemail="5067626010616260362023" href="/cdn-cgi/l/email-protection">[email protected]</a><script data-cfhash="f9e31" type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script> 
...     </span> 
... ''') 
>>> deobfuscate_cf_email(soup) 
>>> soup 
<html><body><span>video resolution</span>, 
    <span class="even">2160p@30fps - 1080p@30fps - 720@120fps 
</span> 
</body></html>