我希望使用PHP从这个网站获得一些随机段落: http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx
启动“提交”按钮的最短代码集(不需要填写文本字段)是什么,只解析得到的随机文本?
似乎无法让提交作品发挥作用。
答案 0 :(得分:1)
您只需要提取span.randomSentence
内容即可。
This可以帮助您做到这一点。
修改强>
代码我测试过:
$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];
}
答案 1 :(得分: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];
}