0

我想知道如何复制“ http://news.google.com/?ned=us&topic=t ”中的“?ned=us&topic=t”部分。基本上,我想复制 url 的路径,或者“.com”之后的部分。我该怎么做呢?

public class Example  {
public static String url = "http://news.google.com/?ned=us&topic=t";
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
            driver.get(url);
            WebElement reportCln=driver.findElement(By.id("id_submit_button"));
            String path=driver.getCurrentUrl();
            System.out.println(path);
}
}
4

3 回答 3

2

您应该看看java.net.URLgetPath()类及其getQuery()方法。

@Test
public void urls() throws MalformedURLException {
    final URL url = new URL("http://news.google.com/?ned=us&topic=t");

    assertEquals("ned=us&topic=t", url.getQuery());
    assertEquals("?ned=us&topic=t", "?" + url.getQuery());
    assertEquals("/", url.getPath());
}

正则表达式很有趣,但 IMO 这更容易理解。

于 2013-06-19T21:50:34.203 回答
1

您可以使用正则表达式来提取您想要的部分:

String txt = "http://news.google.com/?ned=us&topic=t";

String re1 = "(http:\\/\\/news\\.google\\.com\\/)"; // unwanted part
String re2 = "(\\?.*)"; // wanted part

Pattern p = Pattern.compile(re1 + re2, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
    String query = m.group(2);
    System.out.print(query);
}
于 2013-06-19T21:53:10.430 回答
1

尝试这个:

String request_uri = null;
String url = "http://news.google.com/?ned=us&topic=t";

if (url.startsWith("http://") {
    request_uri = url.substring(7).split("/")[1];
} else {
    request_uri = url.split("/")[1];
}

System.out.println (request_uri); // prints: ?ned=us&topic=t

如果您只对查询字符串感兴趣,即google.com/search?q=key+words您想忽略,那么直接search?拆分?

// prints: q=key+words
System.out.println ("google.com/search?q=key+words".split("\\?")[0]);
于 2013-06-19T21:40:09.127 回答