[英]Scrape text inside dynamic element with no id, class, attributes, etc
The only thing I've got going for me is that the preceding <td>
will always have the same (and unique to the document) contents: 我唯一能做的就是前面的
<td>
总是具有相同的(并且是文档的唯一的)内容:
<td>
<label>unique text<label>
</td>
<td>dynamic text</td>
I can easily grab it with jQuery in the browser console (the page has jQuery loaded): 我可以在浏览器控制台中轻松地使用jQuery抓取它(页面加载了jQuery):
$("label:contains('unique text')").parent().next().text();
I've been at this for a while and have tried everything I can think of. 我已经在这一段时间了,并尝试了我能想到的一切。
My most recent attempt was to use casperjs' evaluate and: 我最近的尝试是使用casperjs的评估和:
casper.thenEvaluate(function addID() {
$("label:contains('unique text')").parent().next().attr('id', 'uniqueID');
});
casper.then(function getText() {
var target = this.getHTML('td#uniqueID');
this.echo(target);
});
Which gives me: 这给了我:
CasperError: No element matching selector found: td#uniqueID
CasperError:找不到元素匹配选择器:td#uniqueID
Why is my casper.thenEvaluate
function not creating the td#uniqueID
that I'm looking for? 为什么我的
casper.thenEvaluate
函数没有创建我正在寻找的td#uniqueID
?
If I do it like this post's answer : 如果我喜欢这篇文章的回答 :
casper.then(function getText() {
this.evaluate(function addID() {
$("label:contains('unique text')").parent().next().attr('id', 'uniqueID');
});
var target = this.thenEvaluate(function returnText() {
return $('#uniqueID').text();
});
this.echo(target);
});
I get an [Object Casper]
which seems to be exactly what it sounds like. 我得到了一个
[Object Casper]
,它看起来就像听起来一样。 It's filled with waitForContent
, scrollTo
, etc... 它充满了
waitForContent
, scrollTo
等......
note: The above code block was incorrect (as was pointed out in this answer by Artjom B.) and was changed to this: 注意:上面的代码块不正确(正如 Artjom B的 回答中所指出的那样 )并且改为:
casper.then(function getText() {
this.evaluate(function addID() {
$("label:contains('unique text')").parent().next().attr('id', 'uniqueID');
});
var target = this.fetchText('#uniqueID');
this.echo(target);
});
The problem still persisted. 问题仍然存在。 See my answer below for the resolution.
请参阅下面的答案以获得解决方案。
If you already tried it like in the linked answer, why not just copy it? 如果您已经在链接的答案中尝试过它,为什么不复制它? Your error is that you use
thenEvaluate
inside the then
block. 您的错误是您在
then
块中使用thenEvaluate
。 CasperJS works in steps and you scheduled a step where it is not necessary. CasperJS分步工作,您安排了一个不必要的步骤。 This creates another step which is executed later.
这创建了另一个稍后执行的步骤。
Change thenEvaluate
to evaluate
and it should work fine. 更改
thenEvaluate
来evaluate
,它应该工作的罚款。 While you're at it, you could combine the two: 当你在它时,你可以将两者结合起来:
casper.then(function getText() {
var target = this.evaluate(function addID() {
$("label:contains('unique text')").parent().next().attr('id', 'uniqueID');
return $('#uniqueID').text();
});
this.echo(target);
});
or even 甚至
casper.then(function getText() {
this.evaluate(function addID() {
$("label:contains('unique text')").parent().next().attr('id', 'uniqueID');
});
var target = this.fetchText(#uniqueID);
this.echo(target);
});
I finally got it resolved. 我终于解决了。 When I visit the page in a browser, I click a link and javascript calls a modal via ajax POST with the information I'm grabbing.
当我在浏览器中访问该页面时,我点击一个链接,javascript通过ajax POST调用一个模态,其中包含我正在抓取的信息。 I assumed that in casperjs, it would work the same way when I use:
我认为在casperjs中,当我使用时,它会以相同的方式工作:
casper.thenOpen('http://www.website.net/details.php', {
method: 'post',
data: {
'id': 'foo',
'key': 'bar',
}
});
It looks like casper renders this slightly differently than I had anticipated. 看起来casper的呈现方式与我预期的略有不同。 Even though the main page had jQuery loaded, the modal did not.
即使主页面加载了jQuery,模态也没有。 So I needed:
所以我需要:
clientScripts: ["jquery-2.1.1.min.js"]
Inside my: 我的内心:
var casper = require('casper').create({
});
(casperjs docs on jQuery) (关于jQuery的casperjs文档)
Here is the full script for reference: 以下是完整的脚本供参考:
var url = "http://www.website.net/search.php?key=foobarfoobarfoobarfoobar";
var casper = require('casper').create({
clientScripts: ["jquery-2.1.1.min.js"]
});
casper.start(url, function() {
//some other unrelated stuff is going to go here
});
casper.thenOpen('http://www.website.net/details.php', {
method: 'post',
data: {
'id': 'foo',
'key': 'bar',
}
});
casper.then(function getText() {
this.evaluate(function addID() {
$("label:contains('unique text')").parent().next().attr('id', 'uniqueID');
});
var target = this.fetchText('#uniqueID');
this.echo(target);
});
casper.run();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.