我想知道是否可以“自动化”输入条目来搜索表单并从结果中提取匹配的任务。例如,我有一份期刊文章列表,我希望获得DOI(数字对象标识符);手动为此我会去期刊文章搜索页面(例如,http://pubs.acs.org/search/advanced),键入作者/标题/卷(等),然后从返回的结果列表中找到文章,然后挑出DOI并粘贴那到我的参考名单。我经常使用R和Python进行数据分析(我受到了RCurl的一篇文章的启发),但对网络协议知之甚少......是否有这种可能(例如使用Python的BeautifulSoup之类的东西?)。有没有什么好的参考资料可以做类似于这个任务的任何事情?我同样对学习关于网络抓取和一般网络抓取工具以及完成此特定任务感兴趣......感谢您的时间!网络抓取填写(和检索)搜索表单?
回答
美丽的汤是伟大的解析webpages-是(从乔Albahri的“C#一言以蔽之”改编)是的,你想做什么的一半。 Python和Perl和Ruby都有一个版本机械化的,那就是另一半:
http://wwwsearch.sourceforge.net/mechanize/
机械化让你控制浏览器:
# Follow a link
browser.follow_link(link_node)
# Submit a form
browser.select_form(name="search")
browser["authors"] = ["author #1", "author #2"]
browser["volume"] = "any"
search_response = br.submit()
随着机械化和美丽的汤你有伟大的开始。一个额外的工具,我会考虑的是Firebug的,因为这快速红宝石刮指南中使用:
http://www.igvita.com/2007/02/04/ruby-screen-scraper-in-60-seconds/
萤火虫可以加快您来解析文档,为您节省一些严重的时间的XPath的建设。
祝你好运!
伟大的!谢谢 - 非常有帮助! – hatmatrix 2009-07-23 19:40:58
WebRequest req = WebRequest.Create("http://www.URLacceptingPOSTparams.com");
req.Proxy = null;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
//
// add POST data
string reqString = "searchtextbox=webclient&searchmode=simple&OtherParam=???";
byte[] reqData = Encoding.UTF8.GetBytes (reqString);
req.ContentLength = reqData.Length;
//
// send request
using (Stream reqStream = req.GetRequestStream())
reqStream.Write (reqData, 0, reqData.Length);
string response;
//
// retrieve response
using (WebResponse res = req.GetResponse())
using (Stream resSteam = res.GetResponseStream())
using (StreamReader sr = new StreamReader (resSteam))
response = sr.ReadToEnd();
// use a regular expression to break apart response
// OR you could load the HTML response page as a DOM
谢谢 - 很高兴知道这是可能的! ...我在猜测。 (不太熟悉.NET,虽然我听到它是所有的愤怒...) – hatmatrix 2009-07-23 19:42:08
有很多网页抓取工具。有一个很好的叫做iMacros的firefox插件。它效果很好,完全不需要编程知识。免费版可以从这里下载: https://addons.mozilla.org/en-US/firefox/addon/imacros-for-firefox/ 关于iMacros的最好的事情是,它可以让你在几分钟内开始,它也可以从bash命令行启动,也可以从bash脚本中调用。
更先进的步骤将是硒webdrive。我选择硒的原因在于它适合初学者,是一种很好的方法。阅读下面的内容page:
会让你立即运行。 Selenium支持java,python,php,c,所以如果你熟悉这些语言,你会熟悉所有需要的命令。我更喜欢sed的webdrive变体,因为它会打开浏览器,以便您可以检查字段和输出。使用webdrive设置脚本后,您可以轻松将脚本迁移到IDE,从而无需运行。
通过键入命令
sudo easy_install selenium
这将需要你的依赖和照料一切安装硒可以做。
为了交互运行脚本,只需打开一个终端,并键入
python
你会看到Python提示符,>>>你可以在命令输入。
这里,你可以在终端粘贴示例代码,它将谷歌搜索词奶酪
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}}
我希望这可以给你一个良好的开端。
干杯:)
你有没有想出一个很好的解决这个问题?我在这里问了一个类似的(重复?)问题后发现这个问题http://stackoverflow.com/questions/9711539/can-i-query-the-digital-object-identifier-for-a-list-of-citations – 2012-03-14 23:16:09
@大卫 - 不,不好意思。没有足够的选择来评论... – hatmatrix 2012-03-24 00:51:15