有许多用于网页抓取的工具。有一个很好的 Firefox 插件,叫做 iMacros。它工作得很好,根本不需要编程知识。免费版可以从这里下载:
https://addons.mozilla.org/en-US/firefox/addon/imacros-for-firefox/
iMacros 最棒的地方在于,它可以让你在几分钟内上手,它也可以从 bash 命令行启动,也可以从 bash 脚本中调用。
更高级的步骤是 selenium webdrive。我选择 selenium 的原因是它以非常适合初学者的方式记录在案。阅读以下page:
会让您立即启动并运行。
Selenium 支持 java、python、php、c,所以如果你熟悉这些语言中的任何一种,你就会熟悉所有需要的命令。我更喜欢 selenium 的 webdrive 变体,因为它会打开一个浏览器,以便您检查字段和输出。使用 webdrive 设置脚本后,您可以轻松地将脚本迁移到 IDE,从而无头运行。
要安装 selenium,您可以通过键入命令来完成
sudo easy_install selenium
这将处理依赖项和您所需的一切。
为了以交互方式运行您的脚本,只需打开一个终端,然后输入
python
你会看到python提示符,>>>你可以输入命令。
这是一个示例代码,您可以将其粘贴到终端中,它将在 google 搜索单词 cheeses
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();
}}
我希望这能给你一个良好的开端。
干杯:)