2

我希望使用 PHP 从该站点获取一些随机段落: http ://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx

启动提交按钮(不需要填写文本字段)并仅解析生成的随机文本的最短代码集是什么?

似乎无法让提交作品工作。

4

2 回答 2

1

尝试简单地发布页面。或此网址http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx?_ VIEWSTATE =%2FwEPDwUINTQyOTcxOTkPZBYCZg9kFgQCAg9kFgICAQ9kFgICAQ9kFgQCBw8PFgIeBFRleHRlZGQCCQ8PFgQfAGUeB1Zpc2libGVoZGQCAw8PFgIfAAU4Q29weXJpZ2h0IDIwMDcgd2F0Y2hvdXQ0c25ha2VzLmNvbS4gQWxsIFJpZ2h0cyBSZXNlcnZlZC5kZGTorXr6Gf6R0THMyZRJWZJWtrWHYw%3D%3D& _EVENTVALIDATION =%2FwEWBAKs0KOMDAKf643lBALimIbkBQLx2ZbtAfvuWUuGYHixchu%2FJnnxDjXxcnqg&TMPL%24main%24txtSubject = TMPL%24main%24txtObject = TMPL%24main% 24btnNew=新建+段落

您只需要获取span.randomSentence内容。 可以帮助做到这一点。

编辑
我测试过的代码:

$url = 'http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx?__VIEWSTATE=/wEPDwUINTQyOTcxOTkPZBYCZg9kFgQCAg9kFgICAQ9kFgICAQ9kFgQCBw8PFgIeBFRleHRlZGQCCQ8PFgQfAGUeB1Zpc2libGVoZGQCAw8PFgIfAAU4Q29weXJpZ2h0IDIwMDcgd2F0Y2hvdXQ0c25ha2VzLmNvbS4gQWxsIFJpZ2h0cyBSZXNlcnZlZC5kZGTorXr6Gf6R0THMyZRJWZJWtrWHYw==&__EVENTVALIDATION=/wEWBAKs0KOMDAKf643lBALimIbkBQLx2ZbtAfvuWUuGYHixchu/JnnxDjXxcnqg&tmpl%24main%24txtSubject=&tmpl%24main%24txtObject=&tmpl%24main%24btnNew=New%20Paragraph';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'); 
$c = curl_exec($ch); 
curl_close($ch); 

$p = strpos($c, '<span id="tmpl_main_lblPara" class="randomSentence">');
if ($p !== false) {
    $s = substr($c, $p);
    $pcs = explode('</span>', $s);
    echo $pcs[0];
}
于 2012-10-31T14:41:44.427 回答
1

更灵活的方式 - 动态获取视图状态和事件验证:

<?php
$url = 'http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'); 
$c = curl_exec($ch); 
curl_close($ch); 

//__VIEWSTATE
$viewstate = '';

//__EVENTVALIDATION
$eventvalidation = '';

// Fetch VIEWSTATE
$p1 = '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="';
$p = strpos($c, $p1);
if ($p !== false) {
    $s = substr($c, $p+strlen($p1));
    $pcs = explode('"', $s);
    if (!empty($pcs[0])) {
        $viewstate = $pcs[0];
    }
}

// Fetch EVENTVALIDATION
$p1 = '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="';
$p = strpos($c, $p1);
if ($p !== false) {
    $s = substr($c, $p+strlen($p1));
    $pcs = explode('"', $s);
    if (!empty($pcs[0])) {
        $eventvalidation = $pcs[0];
    }
}

// PUT YOUR OBJECT & SUBJECT HERE
$postvalues = array(
    'tmpl$main$txtSubject' => '',
    'tmpl$main$txtObject' => '',
    'tmpl$main$btnNew' => 'New Paragraph',
);

$url = 'http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx?__VIEWSTATE='.urlencode($viewstate).'&__EVENTVALIDATION='.urlencode($eventvalidation).'&tmpl%24main%24txtSubject='.urlencode($postalues['tmpl$main$txtSubject']).'&tmpl%24main%24txtObject='.urlencode($postalues['tmpl$main$txtObject']).'&tmpl%24main%24btnNew=New%20Paragraph';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'); 
$c = curl_exec($ch); 
curl_close($ch); 


$t = '<span id="tmpl_main_lblPara" class="randomSentence">';
$p = strpos($c, $t);
if ($p !== false) {
    $s = substr($c, $p + strlen($t));
    $pcs = explode('</span>', $s);


   echo $pcs[0]; 
}
于 2012-10-31T16:08:21.543 回答