开发者

How can I use a URL's search engine to return the result of the search?

I've read some tutorials, and I understand what's going on here: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html

My question is, how can I use a URL or URLConnection to access the result of a search. For instance, if my URL was:

URL url = new URL("http://www.stackoverflow.com/");     

how could I access the search-engine at the top of the page to return the results o开发者_StackOverflowf a search?


You should check the page source. It will contain a <form> element. Its action property will hold the URL of the search script. You also have to send your keywords using the name defined in the <input> tag inside the form.

Using SO as the example:

                <form id="search" action="/search" method="get">

                <div>

                    <input name="q" class="textbox" tabindex="1" onfocus="if (this.value=='search') this.value = ''" type="text" maxlength="140" size="28" value="search">

                </div>

                </form>

Will give the following URL:

http://www.stackoverflow.com/search?q=your+keywords+here

You have to encode your search terms using URL encoding. The very basic thing here is to substitute spaces with the character +.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜