Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我似乎无法在python中处理来自regex(re.search)的空白结果,我要么得到重复的结果,要么没有结果?_Python_Regex_Pandas_Beautifulsoup_Python Requests Html - Fatal编程技术网

我似乎无法在python中处理来自regex(re.search)的空白结果,我要么得到重复的结果,要么没有结果?

我似乎无法在python中处理来自regex(re.search)的空白结果,我要么得到重复的结果,要么没有结果?,python,regex,pandas,beautifulsoup,python-requests-html,Python,Regex,Pandas,Beautifulsoup,Python Requests Html,我正试图从中提取个人列表。一旦我有了名单,我会浏览每个成员的链接,并试图找到他们的电子邮件地址 一些成员没有电子邮件,因此代码失败。我尝试添加匹配结果为“无”的代码,在这种情况下,我得到了重复的结果 我使用以下逻辑进行匹配 mat = re.search(r'mailto:\w*\.\w*@parl.gc.ca',ln1.get('href')) if mat: email.append(mat.group()) else: email.appen

我正试图从中提取个人列表。一旦我有了名单,我会浏览每个成员的链接,并试图找到他们的电子邮件地址

一些成员没有电子邮件,因此代码失败。我尝试添加匹配结果为“无”的代码,在这种情况下,我得到了重复的结果

我使用以下逻辑进行匹配

mat = re.search(r'mailto:\w*\.\w*@parl.gc.ca',ln1.get('href'))
    if mat:
        email.append(mat.group())
    else:
        email.append("No Email Found")
如果条件是问题所在。当我使用else时,它会为每行显示一次“未找到电子邮件”

weblinks=[]
email=[]

page = requests.get('https://www.ourcommons.ca/Parliamentarians/en/members?view=ListAll')
soup = BeautifulSoup(page.content, 'lxml')


for ln in soup.select(".personName > a"):
    weblinks.append("https://www.ourcommons.ca" + ln.get('href'))
    if(len(weblinks)==10):
        break  
提取电子邮件
预期结果:为有一个的页面显示电子邮件,为没有的页面显示空白。

如果检查页面
DOM
存在
两个类似的元素,这就是为什么您会得到多个值。您需要设置条件以消除该问题。请尝试下面的代码

weblinks=[]
email=[]

page = requests.get('https://www.ourcommons.ca/Parliamentarians/en/members?view=ListAll')
soup = BeautifulSoup(page.content, 'lxml')


for ln in soup.select(".personName > a"):
    weblinks.append("https://www.ourcommons.ca" + ln.get('href'))
    if(len(weblinks)==10):
        break


for elnk in weblinks:
    pagedet = requests.get(elnk)
    soupdet = BeautifulSoup(pagedet.content, 'lxml')
    if len(soupdet.select(".caucus > a"))> 1:
       for ln1 in soupdet.select(".caucus > :not(a[target])"):
          mat = re.search(r'mailto:\w*\.\w*@parl.gc.ca',ln1.get('href'))
          if mat:
            email.append(mat.group())
          else:
            email.append("No Email Found")
    else:
       for ln1 in soupdet.select(".caucus > a"):
         mat = re.search(r'mailto:\w*\.\w*@parl.gc.ca', ln1.get('href'))
         if mat:
             email.append(mat.group())
         else:
             email.append("No Email Found")

print(email)
print("Len Email:",len(email))
输出:

['mailto:Ziad.Aboultaif@parl.gc.ca', 'mailto:Dan.Albas@parl.gc.ca', 'mailto:harold.albrecht@parl.gc.ca', 'mailto:John.Aldag@parl.gc.ca', 'mailto:Omar.Alghabra@parl.gc.ca', 'mailto:Leona.Alleslev@parl.gc.ca', 'mailto:dean.allison@parl.gc.ca', 'No Email Found', 'No Email Found', 'mailto:Gary.Anand@parl.gc.ca']
Len电子邮件:10


你的代码似乎对我有用。您使用的是什么版本的Python和beautifulsoup?重复结果是什么意思?这是否意味着,当您收到两封相同的电子邮件时,它是匹配的,而当未找到匹配时,您收到两封
“未找到电子邮件”
['mailto:Ziad.Aboultaif@parl.gc.ca', 'mailto:Dan.Albas@parl.gc.ca', 'mailto:harold.albrecht@parl.gc.ca', 'mailto:John.Aldag@parl.gc.ca', 'mailto:Omar.Alghabra@parl.gc.ca', 'mailto:Leona.Alleslev@parl.gc.ca', 'mailto:dean.allison@parl.gc.ca', 'No Email Found', 'No Email Found', 'mailto:Gary.Anand@parl.gc.ca']