【问题标题】:Find German Telefone numbers with Selenium (Java)使用 Selenium (Java) 查找德国电话号码
【发布时间】:2019-04-04 08:33:35
【问题描述】:

我正在尝试通过带有 Selenium Web 驱动程序 API 的正则表达式从德国网站获取 Telefon 号码(德语格式)。我的问题是我得到了一些误报,我还不能排除这些。有人可以帮我优化正则表达式吗?所以我确定我得到的是 100% 的电话号码。在代码中,Impressum 通常是联系方式的名称,这就是为什么我在网站上寻找“Impressum”这个词,然后单击它,然后下载字符串中的 html 正文。然后我使用正则表达式在 html 正文中查找电话号码。谢谢。

  public void search() {
        jse = (JavascriptExecutor) driver;
        WebElement w = driver.findElement(By.partialLinkText("mpress"));
        if (w.getText().matches("Impressum" ) || w.getText().matches("impressum")){
            w.click();
        }
        impressum.setBody(driver.findElement(By.tagName("body")).getText());   // HTML-body download
    }

    @SuppressWarnings("Duplicates")
    public void TelRegex() {
        final String regex = "([\\+][0-9]{1,3} [ \\.\\-\\/])?  ([\\(][0-9]{1,6}[\\)])?  ([0-9 \\.\\-\\/]{10,20})$";
        final String string = impressum.getBody();

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
        }
    }

在代码中,Impressum 通常是联系方式的名称,这就是为什么我在网站上寻找“Impressum”这个词,然后单击它,然后下载字符串中的 html 正文。然后我使用正则表达式在 html 正文中查找电话号码。它给了我 fone 号码,但有时也有其他号码不是 fone 号码。

【问题讨论】:

  • 请分享网址或html源代码。
  • 这是我打开的 URL:vario-doser.de

标签: selenium web-scraping webdriver web-crawler


【解决方案1】:

类:

package syed;

import java.util.ArrayList;
import java.util.Objects;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Syed {
    private static WebDriver driver;    

    @BeforeClass
    public static void setUpClass() {       
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\selenium-tests\\GCH_driver\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("user-data-dir=C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data");
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();        
    }
    @Before public void setUp() {} @After public void tearDown() {}
    @AfterClass public static void tearDownClass() {
        driver.quit();
    }
    @Test
    public void extractAllPhoneNumbers() {
        ArrayList<String> phoneNumbers = new ArrayList<String>();

        driver.get("https://www.vario-doser.de/");
        WebElement impressumLink = waitSec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='ueber-uns/impressum/']")));
        impressumLink.click();
        WebElement content = waitSec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("content")));
        String[] contentText = content.getText().split("\\n");

        for (String line: contentText) {
            if (line.length() > 0 && Objects.equals(line.substring(0, 3), "Tel")) {
                phoneNumbers.add(line);
                System.out.println("Extracting: " + line);
            }
            else {
                System.out.println("Textline does not beginn with 'Tel'");
            }
        }
        if (phoneNumbers.size() > 0) {
            System.out.println("Extracted phone numbers:");
            for (String phoneNumber: phoneNumbers) {
                System.out.println(phoneNumber);
            }
        }
        else {
            System.out.println("No phone number found.");
        }

    }   
    public WebDriverWait waitSec(WebDriver driver, int sec) {
        return new WebDriverWait(driver, sec);
    }
}

输出:

Starting ChromeDriver 2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5) on port 3253
Only local connections are allowed.
Dub 15, 2019 9:46:23 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS

Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Extracting: Tel.: 09721 / 533404
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Extracting: Tel: 0180 / 60 05 85 0
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Textline does not beginn with 'Tel'
Extracted phone numbers:
Tel.: 09721 / 533404
Tel: 0180 / 60 05 85 0

这是你需要的吗?

【讨论】:

  • 不,这不是我要找的。因为在这种情况下,只会下载那些以字符串“Tel”开头后跟数字的 fone 号码,但我想下载所有可用于德国电话号码的格式,以便下载每个 fone 号码 - 无论在哪个格式它被写入。你明白我的意思吗?
【解决方案2】:

根据前缀提取电话号码:

public void extractAllPhoneNumbers() {
    ArrayList<String> phoneNumbers = new ArrayList<String>();

    driver.get("https://www.vario-doser.de/");
    WebElement impressumLink = waitSec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='ueber-uns/impressum/']")));
    impressumLink.click();
    WebElement content = waitSec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("content")));
    String[] contentText = content.getText().split("\\n");

    String[] prefixes = {"0180 / ", "09721 / "};

    for (String line: contentText) {
        for (String prefix: prefixes) {
            if (line.contains(prefix)) {
                phoneNumbers.add(line);
                System.out.println("Extracting: " + line.split(prefix)[1]);
            }
            else {
                System.out.println("Textline does not contain any of the prefixes.");
            }
        }
    }
    if (phoneNumbers.size() > 0) {
        System.out.println("Extracted phone numbers:");
        for (String phoneNumber: phoneNumbers) {
            System.out.println(phoneNumber);
        }
    }
    else {
        System.out.println("No phone number found.");
    }

}

但它包括传真。

...
Textline does not contain any of the prefixes.
Extracted phone numbers:
Tel.: 09721 / 533404
Fax: 09721 / 533405
Tel: 0180 / 60 05 85 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-22
    • 2020-03-19
    • 1970-01-01
    • 2015-01-19
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多