当前位置: 首页 > 知识库问答 >
问题:

python-使用selenium在页面上查找电子邮件地址

蒲功
2023-03-14

我使用的是Python 2.7.13

# -*- coding: utf-8 -*-

from lxml import html
import requests
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

def init_driver():
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 5)
    return driver


def lookup(driver):
    driver.get("http://www.sportbirmingham.org/directory?sport=&radius=15&postcode=B16+8QG&submit=Search")
    try:
        for link in driver.find_elements_by_xpath('//h2[@class="heading"]/a'):
            link.click()
            emailAdress = driver.find_element_by_xpath('//div[@id="widget-contact"]//a‌​').get_attribute('hr‌​ef')
            print emailAdress
    except TimeoutException:
        print "not found"


if __name__ == "__main__":
    driver = init_driver()
    lookup(driver)
    time.sleep(5)
    driver.quit()

当我尝试并继续到链接的下一页时,我得到以下错误

文件“scrape.py”,第43行,在查找(driver)文件“scrape.py”中,第26行,在查找links.extend中([link.get_attribute('href')for link in driver.find_elements_by_xpath('//h2[@class=“heading”]/a')])文件“/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py”,第139行,在get_attribute self,name中)文件“/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”,第465['value']文件“/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”,第236行,在execute self.error_handler.check_response文件“/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”,第192行,在check_response中升起exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.staleElementReferenceException:消息:元素引用已过时。该元素不再附加到DOM或页面已刷新。

共有1个答案

季阳朔
2023-03-14

这似乎是复制/粘贴问题。有时从StackOverflow答案复制代码时,可能会出现一些隐藏字符。Pythonshell中的XPath显示为'//div[@id=“widget-contact”]//a??'。您应该手动重写它,以删除那些??...

还需要注意的是,您的代码不会像您在第一次迭代时那样工作--不能返回到搜索页面。

请尝试使用以下代码:

from selenium.common.exceptions import NoSuchElementException

def lookup(driver):
    driver.get("http://www.sportbirmingham.org/directory?sport=&radius=15&postcode=B16+8QG&submit=Search")
    links = [link.get_attribute('href') for link in driver.find_elements_by_xpath('//h2[@class="heading"]/a')]
    page_counter = 1
    while True:
        try:
            page_counter += 1
            driver.find_element_by_link_text(str(page_counter)).click()
            links.extend([link.get_attribute('href') for link in driver.find_elements_by_xpath('//h2[@class="heading"]/a')])
        except NoSuchElementException:
            break            
    try:
        for link in links:
            driver.get(link)
            try:
                emailAdress = driver.find_element_by_xpath('//div[@id="widget-contact"]//a').text
                print emailAdress
            except NoSuchElementException:
                print "No email specified"
    except TimeoutException:
        print "not found"
 类似资料:
  • 问题内容: 我有兴趣在收到来自具有特定主题的特定地址的电子邮件时触发某些操作。为了能够做到这一点,我需要对邮箱进行监视,检查每个传入的邮件(特别是我使用gmail)。最简单的方法是什么? 问题答案: Gmail提供了通过POP进行连接的功能,您可以在gmail设置面板中将其打开。Python可以使通过POP的连接非常容易: 您只需要将此脚本作为cron作业运行即可。不确定您使用的平台如何,YMMV

  • 问题内容: 如何使用Selenium检查当前页面上是否存在给定的文本字符串? 问题答案: 代码是这样的:

  • 问题内容: 有没有一种使用正则表达式检查表单输入的好方法,以确保它是正确的样式电子邮件地址?自昨晚以来一直在搜索,如果它是子域的电子邮件地址,则回答了有关该主题的人们疑问的每个人似乎也有问题。 问题答案: 无关紧要。即使你可以验证该电子邮件地址在语法上是有效的,你仍然需要检查该电子邮件地址是否未键入错误,以及该地址是否确实属于你认为确实有用的人。唯一的方法是向他们发送电子邮件,并让他们单击链接进行

  • 问题内容: 我正在编写一个Python脚本来处理Procmail返回的电子邮件。如该问题中所建议,我正在使用以下Procmail配置: 我的process_mail.py脚本正在通过stdin接收电子邮件,如下所示: 我正在尝试以这种方式解析消息: 我想获取诸如“发件人”,“收件人”和“主题”之类的消息字段。但是,消息对象不包含任何这些字段。 我究竟做错了什么? 问题答案: 您必须确保这些行不会意

  • 问题内容: 如何使用Python在电子邮件中发送HTML内容?我可以发送简单的文字。 问题答案: 这是一个如何使用替代纯文本版本创建HTML消息的示例: