有时使用CasperJS表单填写和捕获失败(Form fill and capture fail sometimes using CasperJS)
我想填写表单并捕获生成的页面。 所以我写了一个CasperJS(这是我第一次使用CasperJS)归档到目标。 我阅读了CasperJS API文档,但仍不确定如何解决问题。
以下是我的代码:
var casper = require('casper').create(); var filename = casper.cli.get(0); var myear = casper.cli.get(1); var mmon = casper.cli.get(2); var stk_no = casper.cli.get(3); casper.start('http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/STOCK_DAYMAIN.php', function() { this.fill("form[name='date_form']", { 'myear' : myear, 'mmon' : mmon, 'STK_NO': stk_no },true); }); casper.then(function() { this.capture(filename); this.echo("Saved screenshot of " + (this.getCurrentUrl()) + " to " + filename); }); casper.run();
我的命令就像
casperjs test.js picture.png 2013 12 8271
有时候不会生成图片文件,结果证明是失败的:
Saved screenshot of http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/STOCK_DAY.php to picture.png
有时会生成一个图片文件,结果证明是成功的:
Saved screenshot of http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/genpage/Report201312/201312_F3_1_8_8271.php?STK_NO=8271&myear=2013&mmon=12 to picture.png
为什么有时它可以工作并提供捕获但有时会失败? 为什么this.getCurrentUrl()的返回值可能不同?
I want to fill the form and take capture of the generated page. So I wrote a CasperJS (This is my first time to use CasperJS) to archive to goal. I read the CasperJS API document but still not sure how to fix the problem.
Below is my code:
var casper = require('casper').create(); var filename = casper.cli.get(0); var myear = casper.cli.get(1); var mmon = casper.cli.get(2); var stk_no = casper.cli.get(3); casper.start('http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/STOCK_DAYMAIN.php', function() { this.fill("form[name='date_form']", { 'myear' : myear, 'mmon' : mmon, 'STK_NO': stk_no },true); }); casper.then(function() { this.capture(filename); this.echo("Saved screenshot of " + (this.getCurrentUrl()) + " to " + filename); }); casper.run();
My command is like
casperjs test.js picture.png 2013 12 8271
Sometimes no picture file will be generated and it turns out to be failed:
Saved screenshot of http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/STOCK_DAY.php to picture.png
Sometimes a picture file will be generated and it turns out to be successful:
Saved screenshot of http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/genpage/Report201312/201312_F3_1_8_8271.php?STK_NO=8271&myear=2013&mmon=12 to picture.png
Why does sometime it work and give the capture but sometimes fail? Why the return value of this.getCurrentUrl() could differ?
原文:https://stackoverflow.com/questions/20998350
最满意答案
以下是此帖后的解决方案。 它使用
edit_colors()
包中的edit_colors()
函数。 (免责声明:我是该套餐的作者。)p <- ggplot(economics) + aes(unemploy, psavert) + geom_point() + geom_smooth(se = F) + ggtitle('Unemploy vs Personal Savings Rate') library(colorblindr) # devtools::install_github("clauswilke/colorblindr") library(colorspace) # install.packages("colorspace", repos = "http://R-Forge.R-project.org") --- colorblindr requires the development version # need also install cowplot; current version on CRAN is fine. # modify alpha values using the alpha function from the scales package p_alpha <- edit_colors(p, scales::alpha, alpha = .5) # print grid::grid.newpage() grid::grid.draw(p_alpha)
缩放的
alpha()
函数的一个缺点是它替换了alpha值,它没有组合alpha值。 所以这里有一个简单的例子,说明如何使用自己的alpha组合函数组合alpha值:mult_alpha <- function(color, alpha = .5) { col <- grDevices::col2rgb(color, TRUE)/255 new_col <- grDevices::rgb(col[1, ], col[2, ], col[3, ], alpha*col[4, ]) new_col[is.na(color)] <- NA new_col } p2 <- ggplot(iris, aes(Sepal.Length, fill = Species)) + geom_density(alpha = .3) + theme_bw() p2_mult_alpha <- edit_colors(p2, mult_alpha, alpha = .7)
两种方法之间的比较表明,它们对已经具有一定透明度的图形给出了截然不同的结果:
p2_alpha <- edit_colors(p2, scales::alpha, alpha = .7) cowplot::plot_grid(p2_alpha, p2_mult_alpha)
(左图:使用尺度的
alpha
;右图:使用mult_alpha
定义的mult_alpha
。)Here is a solution following this post. It uses the
edit_colors()
function from the colorblindr package. (Disclaimer: I'm an author of that package.)p <- ggplot(economics) + aes(unemploy, psavert) + geom_point() + geom_smooth(se = F) + ggtitle('Unemploy vs Personal Savings Rate') library(colorblindr) # devtools::install_github("clauswilke/colorblindr") library(colorspace) # install.packages("colorspace", repos = "http://R-Forge.R-project.org") --- colorblindr requires the development version # need also install cowplot; current version on CRAN is fine. # modify alpha values using the alpha function from the scales package p_alpha <- edit_colors(p, scales::alpha, alpha = .5) # print grid::grid.newpage() grid::grid.draw(p_alpha)
One downside of the
alpha()
function from scales is that it replaces the alpha value, it doesn't combine alpha values. So here is a simple example of how you can combine alpha values, using your own alpha combination function:mult_alpha <- function(color, alpha = .5) { col <- grDevices::col2rgb(color, TRUE)/255 new_col <- grDevices::rgb(col[1, ], col[2, ], col[3, ], alpha*col[4, ]) new_col[is.na(color)] <- NA new_col } p2 <- ggplot(iris, aes(Sepal.Length, fill = Species)) + geom_density(alpha = .3) + theme_bw() p2_mult_alpha <- edit_colors(p2, mult_alpha, alpha = .7)
Comparison between the two approaches shows that they give very different results for plots that already have some transparency:
p2_alpha <- edit_colors(p2, scales::alpha, alpha = .7) cowplot::plot_grid(p2_alpha, p2_mult_alpha)
(left image: using
alpha
from scales; right image: usingmult_alpha
as defined above.)
相关问答
更多-
正如Joris和Chase已经正确回答的那样,标准的最佳做法是简单地省略meansdf$部分并直接引用数据框的列。 testplot <- function(meansdf) { p <- ggplot(meansdf, aes(fill = condition, y = means, x = condition)) p + geom_bar(position = "dodge", stat = " ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如何创建ggplot的列表(How to create lists of ggplot)[2022-07-15]
library(ggplot2) p = c(qplot(1:10,rnorm(10))) p2 = qplot(1:10,rnorm(10)) p[[1]] (与p[['data']]) )应该是一个dataframe 。 它通常包含该图的数据。 p是一个list因为你使用了c函数。 p2是一个ggplot因为这就是qplot返回的结果。 看看每个对象的属性。 attributes(p) # $names # [1] "data" "layers" "scales" " ... -
如何使用ggplot2在R中使用透明背景制作图形?(How to make graphics with transparent background in R using ggplot2?)[2022-08-03]
除panel.background之外,还有一个plot.background选项: df <- data.frame(y=d,x=1) p <- ggplot(df) + stat_boxplot(aes(x = x,y=y)) p <- p + opts( panel.background = theme_rect(fill = "transparent",colour = NA), # or theme_blank() panel.grid.minor = theme_blank() ... -
从ggplot2的Remove Plot Margins找到答案 theme(plot.margin=grid::unit(c(0,0,0,0), "mm")) 做这项工作 Found the answer from Remove Plot Margins in ggplot2 theme(plot.margin=grid::unit(c(0,0,0,0), "mm")) does the job
-
你需要在png指定尺寸和分辨率。 这对我有用 png("graph.png", width=20, height=15, units="cm", res=800, bg="transparent") gridExtra::grid.arrange(o1, o2, ncol=1) dev.off() 你也可以使用ggsave 。 这里我使用cowplot::plot_grid来组合o1和o2 o3 <- cowplot::plot_grid(o1, o2, nrow = 2) ggsave(plo ...
-
在R中创建一个函数,使所有ggplot组件更加透明(Create a function in R to make all the ggplot components more transparent)[2022-11-25]
以下是此帖后的解决方案。 它使用edit_colors()包中的edit_colors()函数。 (免责声明:我是该套餐的作者。) p <- ggplot(economics) + aes(unemploy, psavert) + geom_point() + geom_smooth(se = F) + ggtitle('Unemploy vs Personal Savings Rate') library(colorblindr) # devtools::install_github(" ... -
除了使用变量名称外,还存在范围问题。 GGPlot在全局环境中执行非标准评估,因此您的函数中定义的任何内容都不能直接访问,除了“数据”参数,因为该参数是明确传递而不是通过非标准评估。 因此,解决此问题的方法是将您的变量添加到data参数中。 我创建了一个我认为模仿你的问题的例子,但由于我没有你的数据,所以它不相同: gg_fun <- function() { data <- data.frame(a=1:10, b=1:10) clr <- rep(c("a", "b"), 5) ggplot ...
-
正如@baptiste所指出的,你应该使用aes_string()而不是aes()来定义x和y值。 您还应该将value和variable放在引号内。 PropBarPlot<-function(df, mytitle=""){ melteddf<-melt(df, id=names(df)[1], na.rm=T) ggplot(melteddf, aes_string(x=names(df)[1],y= "value", fill="variable")) + geom_bar(pos ...
-
使用::运算符来指定要调用ggplot2在ggplot2包中的函数的版本,而不是您在刚才在全局工作区中创建的版本。 即类似的东西 ggplot <- function(...) { ggplot2::ggplot(...) + theme_bw() } 应该工作(虽然我没有测试过!) 我也很喜欢theme_bw() 。 我这样做的方式是在加载包后立即使用theme_set() ,例如 library("ggplot2"); theme_set(theme_bw()) 这可以说比你的解决方案更容易,更 ...