【问题标题】:BeautifulSoup email extraction not workingBeautifulSoup 电子邮件提取不起作用
【发布时间】:2017-03-06 05:54:12
【问题描述】:

我编写了一个基本脚本来从网页中提取电子邮件。

from bs4 import BeautifulSoup
import requests, re

def get_email(url):
    response = requests.get(url, headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
        'Upgrade-Insecure-Requests': '1', 'x-runtime': '148ms'}, allow_redirects=True).content

    soup = BeautifulSoup(response, "html.parser")

    email = soup(text=re.compile(r'^[a-zA-Z]+[\w\-.]+@[\w-]+\.[\w.-]+[a-zA-Z]')) # this is working with

    print ("email ",email)


get_email('http://www.aberdeenweddingshop.co.uk/contact-us')
get_email('http://www.foodforthoughtdeli.co.uk/contact.htm')

OUTPUT:  
email  info@aberdeenweddingshop.co.uk
email  [] <------------------------#should give info@foodforthoughtdeli.co.uk

它为第一个 URL 提供了正确的结果,但没有在第二个 URL 中获取任何内容。我不知道原因。我也尝试更改正则表达式。我验证了正则表达式here,但由于某种原因它不能在代码中工作。

【问题讨论】:

    标签: regex python-3.x beautifulsoup


    【解决方案1】:

    在您的第一种情况下,电子邮件是来自单个跨度的文本。在您的第二种情况下,电子邮件位于 p 元素中,其文本比您的电子邮件更多。

    您的正则表达式不会在您的第二个匹配,因为您正在搜索字符串的开头以及在给定上下文中无效的字符。

    您必须在 in 字符串中找到您的电子邮件,然后提取它。 示例:

    from bs4 import BeautifulSoup
    import requests, re
    
    def get_email(url):
        response = requests.get(url, headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
        'Upgrade-Insecure-Requests': '1', 'x-runtime': '148ms'}, allow_redirects=True).content
    
        soup = BeautifulSoup(response, "html.parser")
    
        email = soup(text=re.compile(r'[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*'))
    
        _emailtokens = str(email).replace("\\t", "").replace("\\n", "").split(' ')
    
        if len(_emailtokens):
            print([match.group(0) for token in _emailtokens for match in [re.search(r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", str(token.strip()))] if match])
    
    
    get_email('http://www.aberdeenweddingshop.co.uk/contact-us')
    get_email('http://www.foodforthoughtdeli.co.uk/contact.htm')
    

    输出:

    ['info@aberdeenweddingshop.co.uk']

    ['info@foodforthoughtdeli.co.uk']

    【讨论】:

      【解决方案2】:

      缺少与第二个 URL 匹配的原因是插入符号 (^) 要求正则表达式位于开头。当省略插入符号时,将获得以下结果:

      >>> soup(text=re.compile(r'[a-zA-Z]+[\w\-.]+@[\w-]+\.[\w.-]+[a-zA-Z]'))
      ['E-mail: \n\t\t\t\t\t\t\t\t\t\t\t\t\tinfo@foodforthoughtdeli.co.uk\n\t\t\t\t\t\t\t\t\t\t\t\t\t']
      

      由于我们使用正则表达式来匹配响应中的字符串,我们并没有真正使用 Beautiful Soup 的优点,它可以完全省略:

      def get_email(url):
          response = requests.get(url, headers={
              'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
              'Upgrade-Insecure-Requests': '1', 'x-runtime': '148ms'}, allow_redirects=True).content
          response = requests.get(url, headers = headers, allow_redirects=True).text
          email_address = re.search(r'[a-zA-Z]+[\w\-.]+@[\w-]+\.[\w.-]+[a-zA-Z]', response).group()
          print(email_address)
      

      注意:我使用响应对象的text 属性来处理字符串表示,而不是使用content 属性返回的字节流。

      【讨论】:

        猜你喜欢
        • 2014-07-05
        • 1970-01-01
        • 2017-02-01
        • 2011-05-18
        • 2016-12-14
        • 2015-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多