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

标签 python regex pandas beautifulsoup python-requests-html

我正在尝试从 https://www.ourcommons.ca/Parliamentarians/en/members?view=List 中提取个人列表.获得列表后,我会浏览每个成员链接并尝试找到他们的电子邮件地址。

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

我正在使用以下逻辑进行匹配

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")

if 条件是问题所在。当我使用 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  

提取电子邮件

for elnk in weblinks:
    pagedet = requests.get(elnk)
    soupdet = BeautifulSoup(pagedet.content, 'lxml')
    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("Len Email:",len(email))

预期结果:有的页面显示邮件,没有的页面显示空白。

最佳答案

如果检查页面 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 Email: 10

关于python - 我似乎无法在 python 中处理来自 regex(re.search) 的空白结果,我要么得到重复结果,要么没有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991595/

相关文章:

用于复杂列表和整数组合的 python 正则表达式

python - 将 ConfigParser 值转换为 python 数据类型

Java:需要从字符串中提取数字

regex - .tsv输出文件无法在Hive中正确导入

python - pandas read_table 带有正则表达式分隔符,因为负数没有中间空格

python - 使用 pandas 根据条件替换值

python - Pandas如何根据每组的长度和另一列的计数值计算按组结果

python - 将 2 个数据帧与索引连接为字符串列表时出错

python - 凯拉斯 + tensorflow : 'ConvLSTM2D' object has no attribute 'outbound_nodes'

python - Seaborn fiddle 图与 x 轴标签不对齐