Bash 通过终端控制Firefox
我正在编写一个脚本,其中包括根据用户的需求(我将其作为输入)向用户显示一个网页,并在firefox中打开它。例如,这方面的粗略版本是:Bash 通过终端控制Firefox,bash,firefox,Bash,Firefox,我正在编写一个脚本,其中包括根据用户的需求(我将其作为输入)向用户显示一个网页,并在firefox中打开它。例如,这方面的粗略版本是: #!/bin/bash read -p "What do you want to search" search_term link=$(echo "http://www.mywebsite_whatever.com/search?q="$search_term) firefox $link 主要问题是我无法创建此网站,因为我没有权限。 现在我想做的是: 让用户
#!/bin/bash
read -p "What do you want to search" search_term
link=$(echo "http://www.mywebsite_whatever.com/search?q="$search_term)
firefox $link
主要问题是我无法创建此网站,因为我没有权限。
现在我想做的是:
让用户只查看网页上的某些关键字。因此,我想:
find
框(Ctrl+F)并输入关键字(不更改firefox的源代码)grep
根据需要输出关键字。[请不要一开始就说这是不道德的,我这样做只是为了练习]我正在使用Linux。要控制程序中的浏览器,请尝试。它支持
Java
,Python
等
请参见以下示例源代码:
package org.openqa.selenium.example;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.support.ui.ExpectedCondition;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共类示例{
公共静态void main(字符串[]args){
//创建Firefox驱动程序的新实例
//请注意,代码的其余部分依赖于接口,
//而不是执行。
WebDriver=newfirefoxdriver();
//现在用这个来访问谷歌
驱动程序。获取(“http://www.google.com");
//或者也可以这样做
//驱动程序。导航()。到(“http://www.google.com");
//按名称查找文本输入元素
WebElement=driver.findElement(By.name(“q”));
//输入要搜索的内容
元素。sendKeys(“Cheese!”);
//现在提交表单。WebDriver将从元素中为我们找到表单
元素。提交();
//检查页面的标题
System.out.println(“页面标题为:“+driver.getTitle());
//谷歌的搜索是用JavaScript动态呈现的。
//等待页面加载,10秒后超时
(新的WebDriverWait(驱动程序,10))。直到(新的预期条件(){
公共布尔应用(WebDriver d){
返回d.getTitle().toLowerCase().startsWith(“cheese!”);
}
});
//应该看到:“奶酪!-谷歌搜索”
System.out.println(“页面标题为:“+driver.getTitle());
//关闭浏览器
driver.quit();
}
}
要控制程序中的浏览器,请尝试。它支持Java
,Python
等
请参见以下示例源代码:
package org.openqa.selenium.example;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.support.ui.ExpectedCondition;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共类示例{
公共静态void main(字符串[]args){
//创建Firefox驱动程序的新实例
//请注意,代码的其余部分依赖于接口,
//而不是执行。
WebDriver=newfirefoxdriver();
//现在用这个来访问谷歌
驱动程序。获取(“http://www.google.com");
//或者也可以这样做
//驱动程序。导航()。到(“http://www.google.com");
//按名称查找文本输入元素
WebElement=driver.findElement(By.name(“q”));
//输入要搜索的内容
元素。sendKeys(“Cheese!”);
//现在提交表单。WebDriver将从元素中为我们找到表单
元素。提交();
//检查页面的标题
System.out.println(“页面标题为:“+driver.getTitle());
//谷歌的搜索是用JavaScript动态呈现的。
//等待页面加载,10秒后超时
(新的WebDriverWait(驱动程序,10))。直到(新的预期条件(){
公共布尔应用(WebDriver d){
返回d.getTitle().toLowerCase().startsWith(“cheese!”);
}
});
//应该看到:“奶酪!-谷歌搜索”
System.out.println(“页面标题为:“+driver.getTitle());
//关闭浏览器
driver.quit();
}
}
使用Wget和--user-agent
开关,例如,让网站认为您正在使用Firefox
wget --user-agent="Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:12.0) Gecko/20100101 Firefox/12.0"
当然,对于永久脚本,您应该使用--user agent=“MyScript/1.0”(http://mywebsite/)“
或类似功能,以便在出现问题时他们知道联系谁。使用Wget和--user agent
开关,使网站认为您正在使用Firefox,例如
wget --user-agent="Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:12.0) Gecko/20100101 Firefox/12.0"
当然,对于永久脚本,您应该使用
--user agent=“MyScript/1.0”(http://mywebsite/)“
或类似内容,以便在出现问题时,他们知道联系谁。如果您“没有权限”使用wget/urllib,网站所有者可能不希望您自动让firefox执行相同的操作。如果只是一个练习,那就用wget吧。@Mechanicalsnail。我是说,我不能用wget。它给我的403权限被拒绝。如果你“没有权限”使用wget/urllib,网站所有者可能不希望你自动使用firefox来做同样的事情。如果只是一个练习,那就用wget吧。@Mechanicalsnail。我是说,我不能用wget。它给了我403个拒绝的许可,这正是我想要的。谢谢正是我想要的。谢谢