自动搜索网站字段并获取结果URL

时间:2013-02-15 22:50:48

标签: url web automation

我想在http://www.drugbank.ca/自动搜索字符串并获取结果网址(搜索字段位于页面顶部)。仅通过操纵URL无法搜索网站。是否有基于服务器的方法来做到这一点?我想创建一个带有输入字段的自己的网页和“搜索DrugBank for X并获取URL”的按钮。

感谢。

1 个答案:

答案 0 :(得分:0)

您需要获取以下内容:

http://www.drugbank.ca/search?query=searchstring

您无法使用javascript执行此操作,浏览器不允许查询不同域的网站(由于:http://en.wikipedia.org/wiki/Same_origin_policy)。

我会用php创建一个像searchDrugBank.php这样的文件:

<?php
$urlContent = file_get_contents('http://www.drugbank.ca/search?query=' . $_GET['q']);
// process $urlContent however you want
?>

然后你把它放在你的网站上:

<form method="get" action="searchDrugBank.php">
<input type="text" name="q" />
<input type="submit" value="Search drugbank"/>
</form>

(自您提问)

要找到我要查询的URL我去了网站,看了我按下搜索时提交的表单(查看源代码,或者在其中很容易做到“检查元素”,例如搜索框或搜索按钮)。

我发现表格是:

<form accept-charset="UTF-8" action="/search" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div>
      <strong>Search:</strong> 
      <input id="query" name="query" placeholder="Search DrugBank" size="30" type="search">
      <input name="commit" type="submit" value="Search">&nbsp;
      <a href="/search/advanced">Help / Advanced</a>
</form>

这意味着当您按搜索时,确实发生的事情是您将执行 GET 请求,因为 method =“get”并获取请求意味着要求一个网址,如果需要参数,则它们应该在网址(http://en.wikipedia.org/wiki/Query_string#Web_forms)中。

要查询的网址是 / search ,因为操作是 action =“/ search”其余网址将使用提供的参数构建,这里只是:

<input id="query" name="query" placeholder="Search DrugBank" size="30" type="search">

在那里你可以看到应该提供用于搜索的参数的名称,即“query”

相关问题