我正在抓取一个安全的网站,每当我重新启动我的爬虫应用程序时都会阻止我(我需要将IP更改为技巧).我在Chrome驱动程序中使用默认用户配置文件解决了这个问题(我现在正在使用C#,但如果需要我可以切换到java):
ChromeOptions options = new ChromeOptions();
options.AddArguments($"user-data-dir=C:/Users/{Environment.UserName}/AppData/Local/Google/Chrome/User Data/Default");
Run Code Online (Sandbox Code Playgroud)
它会保存所有会话和cookie,并在重新启动应用程序时恢复它们.一切都按预期工作.
现在,由于某些原因,我需要将我的webdriver更改为PhantomJS.
我的问题如何使用PhantomJS使这种情况成为可能:登录帐户(如gmail或facebook),关闭我的应用程序和驱动程序,在下次运行应用程序和驱动程序时发现自己已登录.换句话说,如何在每次运行时为PhantomJS使用相同的会话?
尝试1(在C#中):
在做了一些搜索之后,我发现这可以使用PhantomJS中的本地存储和cookie文件参数来完成.现在问题是本地存储路径总是空的并且没有保存(我导航到多个站点但仍然是空的),因此,我不能使用先前执行的会话.我设置本地存储和cookie文件的代码很简单,如下所示:
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.LocalStoragePath = Application.StartupPath + "\\default";
service.CookiesFile = Application.StartupPath + "\\default\\Cookies";
IWebDriver driver = new PhantomJSDriver(service);
Run Code Online (Sandbox Code Playgroud)
我的做法有什么问题?
尝试2(在C#中):
根据@SiKing的回答和评论讨论,我改为下面的代码(使用AddArgument),但目录仍为空:
string localStoragePath = Path.Combine(Path.GetTempPath(),"PhantomLocalStorage-");
if (!Directory.Exists(localStoragePath))
{
Directory.CreateDirectory(localStoragePath);
}
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.AddArgument("--local-storage-quota=5000");
service.AddArgument("--local-storage-path=" + localStoragePath);
IWebDriver driver = new PhantomJSDriver(service);
Run Code Online (Sandbox Code Playgroud)
试试3(在java中):
目录仍为空:
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
List<String> cliArgs = new ArrayList<String>();
Path local_storage_path = Paths.get(System.getProperty("java.io.tmpdir") + "PhantomLocalStorage-");
if (Files.notExists(local_storage_path)) {
try { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一些类从CSS文件(如 bootstrap.css)导入到我的 site.scss SASS文件中,而不是全部。以下代码的问题是我在编译的 site.css 文件中获取了所有引导类:
站点.scss
@import "bootstrap";
.my-div-md-6
{
/*some other styles*/
@extend .col-md-6;
}
Run Code Online (Sandbox Code Playgroud)
另一方面,可以通过使用以下代码导入 bootstrap.css 作为参考来使用LESS来完成此操作:
无站点
@import (less, reference) "bootstrap.css";
.my-div-md-6{
/*some other styles*/
&:extend(.col-md-6);
}
Run Code Online (Sandbox Code Playgroud)
LESS的编译输出非常轻,如下所示:
站点.css
.my-div-md-6 {
position: relative;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
}
@media (min-width: 992px) {
.my-div-md-6 {
float: left;
}
.my-div-md-6 {
width: 50%;
}
}
.my-div-md-6 {
/*some other styles*/
}
Run Code Online (Sandbox Code Playgroud)
用SASS可以实现这一点吗?如果是,给出一个简单的例子会有所帮助。
客户端需要哪些 http 请求标头才能从服务器获取正确的响应?是否取决于服务器后端?
我遇到过这样的情况:设置所有请求标头(就像浏览器一样)给了我不正确的响应,我不得不划掉一些。
如果所有请求标头都是必需的,是否意味着编程语言中的 http 帮助程序库设置了一些默认值?如果没有,是否有比错误和跟踪更好的解决方案?