无法从网页中提取电子邮件地址

时间:2018-04-16 12:23:57

标签: python python-3.x web-scraping beautifulsoup

我已经在python中编写了一个脚本,以便从网页中的每个容器中获取一些属性titles及其相应的email地址。当我运行我的脚本时,它只会抓取titles,但在email address的情况下,它只会抓取连接到send eamil按钮的文本。我如何检索那些email addresses,因为当我按下send email button时,它会发送电子邮件。对此的任何帮助将受到高度赞赏。

链接到website

这是我迄今为止所尝试过的:

import requests
from bs4 import BeautifulSoup

URL = "use_above_link"

def Get_Leads(link):
    res = requests.get(link)
    soup = BeautifulSoup(res.text,"lxml")
    for items in soup.select(".media"):
        title = items.select_one(".item-name").text.strip()
        try:
            email = items.select_one("a[alt^='Contact']").text.strip()
        except:
            email = ""
        print(title,email)

if __name__ == '__main__':
    Get_Leads(URL)

结果我有:

Singapore Immigration Specialist SEND EMAIL
Faithful+Gould Pte Ltd SEND EMAIL
PsyAsia International SEND EMAIL
Activpayroll SEND EMAIL
Precursor SEND EMAIL

而不是文字send email,我希望抓住email address

2 个答案:

答案 0 :(得分:6)

网站本身不包含代码中的电子邮件,因此您无法直接删除它们。你能做的是:

  1. 从“访问网站”链接收集公司网站的链接。
  2. 抓取这些网站的主页并搜索是否有任何联系电子邮件地址。
  3. 如果您没有找到任何电子邮件地址,请搜索“与我们联系”页面的链接。
  4. 打开“联系我们”页面,从那里获取电子邮件地址。
  5. 我玩过这个概念,它对我来说非常好,因为我能够抓取许多公司的电子邮件地址。这是我做的:

    报废公司网站的网址

    修改了Get_Lead方法。现在,Get_Lead还将抓取网站网址并调用方法@FXML private AnchorPane batchProcesses_Controller; @FXML private BatchProcesses_Controller batchProcesses_ControllerController; ,该方法会返回电子邮件地址。

    scrape_contact_emails(link)

    报废电子邮件

    以下是从网站上抓取电子邮件地址的方法。首先,它将在主页中搜索电子邮件地址。电子邮件地址很可能出现在主页中,并且必须用于联系目的。如果找不到电子邮件地址,它将搜索“联系我们”页面的URL,并在那里搜索电子邮件地址。

    def Get_Leads(link):
        res = requests.get(link)
        soup = BeautifulSoup(res.text,"lxml")
        for items in soup.select(".media"):
            title = items.select_one(".item-name").text.strip()
            try:
                website = items.select_one("a[alt^='Visit Website']")['href']
            except:
                website = ""
            companies.append([title,website])
            for company,site in companies:
                try:
                    print("Company: "+company+"\nWebsite: "+site+"\n"+scrape_contact_emails(site)+"\n\n--------------------\n\n")
                except:
                    pass
    

    输出

    这是我得到的结果的一小部分。我无法为每家公司提取电子邮件地址,因为有些网站已经对像验证码等机器人进行了保护。我很确定这些代码并不完美,只是一个原型但可以进行很多改进。希望这会帮助你。

    def scrape_contact_emails(link):
        res = requests.get(link)
        domain = link.split(".")
        mailaddr = link
        soup = BeautifulSoup(res.text,"lxml")
        links = soup.find_all("a")
        contact_link = ''
        final_result = ""
        try:
            # Check if there is any email address in the homepage. 
            emails = soup.find_all(text=re.compile('.*@'+domain[1]+'.'+domain[2].replace("/","")))
            emails.sort(key=len)
            print(emails[0].replace("\n",""))
            final_result = emails[0]
        except:
            # Searching for Contact Us Page's url.
            try:
                flag = 0
                for link in links:
                    if "contact" in link.get("href") or "Contact" in link.get("href") or "CONTACT" in link.get("href") or 'contact' in link.text or 'Contact' in link.text or 'CONTACT' in link.text:
                        if len(link.get("href"))>2 and flag<2:
                            flag = flag + 1
                            contact_link = link.get("href")
    
            except:
                pass
    
            domain = domain[0]+"."+domain[1]+"."+domain[2]
            if(len(contact_link)<len(domain)):
                domain = domain+contact_link.replace("/","")
            else:
                domain = contact_link
    
            try:
                # Check if there is any email address in the Contact Us Page. 
                res = requests.get(domain)
                soup = BeautifulSoup(res.text,"lxml")
                emails = soup.find_all(text=re.compile('.*@'+mailaddr[7:].replace("/","")))
                emails.sort(key=len)
                try:
                    print(emails[0].replace("\n",""))
                    final_result = emails[0]
                    return final_result
                except:
                    pass
            except Exception as e:
                pass
    
        return ""
    

答案 1 :(得分:0)

这是因为它自己的网站不包含该公司的HTML电子邮件地址,尝试点击该链接,它会打开自己的电子邮件发送表单给该特定公司。

我会首先收集所有公司的网址,访问每家公司,然后收集所有电子邮件。

html of the company in given link of your's 如您所见,没有电子邮件 当您点击发送电子邮件时,它会打开此公司的网页,然后它会发送我自己的系统,而不是向您显示发送的电子邮件,因为我无法检测到它。 。

我猜他们会在链接上通过ID隐藏电子邮件,href="https://www.angloinfo.com/singapore/directory/listing/enquire/singapore-the-insight-bureau-pte-ltd"当您点击发送电子邮件时,它会打开此页面https://www.angloinfo.com/singapore/directory/listing/enquire/singapore-the-insight-bureau-pte-ltd并在发送消息< / strong>按钮,您可以看到生成的链接,我想指向他们的数据库点,该公司的电子邮件在哪里... action="https://www.angloinfo.com/singapore/directory/listing/sendmessage/singapore-the-insight-bureau-pte-ltd/161535"