python 无法使用请求模块从网页检索电子邮件

wz3gfoph  于 2023-01-29  发布在  Python
关注(0)|答案(1)|浏览(133)

我正在尝试使用requests模块从webpage获取一封电子邮件。问题是,电子邮件地址似乎被编码或什么的,这就是为什么它是不可读的,我想解码它的正常形式。

import requests
from bs4 import BeautifulSoup

link = 'https://global-standard.org/find-suppliers-shops-and-inputs/certified-suppliers/database/search_result/38996'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}

res = requests.get(link,headers=headers)
soup = BeautifulSoup(res.text,"html.parser")
email = soup.select_one("script[type='text/javascript']:-soup-contains('emailProtector')").contents[0]
print(email)

当我运行上面的脚本时,得到了以下结果:

emailProtector.addCloakedMailto("ep_586c4771", 1);

这就是我想要的结果

fttextilegroup2017@gmail.com
zmeyuzjn

zmeyuzjn1#

您可以尝试:

import re
import requests
from bs4 import BeautifulSoup

url = 'https://global-standard.org/find-suppliers-shops-and-inputs/certified-suppliers/database/search_result/38996'

def decloak(cloaked_tag, attr_name):
    a, b = "" , ""
    for span in cloaked_tag.select('span'):
        for attr in span.attrs:
            if attr == attr_name:
                a += span[attr]
            else:
                b = span[attr] + b
    return a + b

soup = BeautifulSoup(requests.get(url).content, 'html.parser')
attr_name = re.search(r'nodeName\.toLowerCase\(\)\.indexOf\("(.*?)"', str(soup)).group(1)
mail = decloak(soup.select_one('.cloaked_email'), attr_name)
print(mail)

图纸:

fttextilegroup2017@gmail.com

相关问题