Php cURL Scrape然后解析/查找特定内容
我正在使用php和cURL来抓取一个网页,但是这个网页设计得很糟糕(因为标签上没有类或ID),所以我需要搜索特定的文本,然后转到持有它的标签(即Php cURL Scrape然后解析/查找特定内容,php,parsing,curl,web-scraping,Php,Parsing,Curl,Web Scraping,我正在使用php和cURL来抓取一个网页,但是这个网页设计得很糟糕(因为标签上没有类或ID),所以我需要搜索特定的文本,然后转到持有它的标签(即),然后移动到下一个子项(或下一个子项)并获取文本 我需要从页面中获取各种信息,其中一些是)中的文本。您可以将问题分为几个部分 正在从数据源检索数据。 为此,您可以根据需要使用CURL或file_get_contents()。代码示例无处不在。及 解析检索到的数据。 为此,我将首先研究“PHP简单HTML DOM解析器”,您可以使用它从HTML字符串中提
),然后移动到下一个子项(或下一个子项)并获取文本
我需要从页面中获取各种信息,其中一些是)中的文本。您可以将问题分为几个部分
正在从数据源检索数据。
为此,您可以根据需要使用CURL或file_get_contents()。代码示例无处不在。及
解析检索到的数据。
为此,我将首先研究“PHP简单HTML DOM解析器”,您可以使用它从HTML字符串中提取数据
构建并生成输出。
这只是一个问题,您想对提取的数据做什么。例如,您可以打印、重新格式化或将其存储到数据库/文件中
我建议你用瑞迪制造的替罪羊。我使用Goutte(),它允许我加载网站内容并以与jQuery相同的方式遍历它。i、 e.如果我想要
的内容,我使用$client->filter(“#content”)->text()
它甚至允许我查找和“点击”链接,并提交表单以检索和处理内容
它比使用cURL或file_get_contentsa()手动浏览html更简单请向我们展示一些代码,以便我们可以帮助您。感谢您的回复,Horaland,Goutte能够搜索纯文本吗?因为这个糟糕的网站没有css类或ID,所以我不得不使用复杂的方法。此外,我还研究了痛风,但它在如何设置上非常混乱。我想这需要大口大口,我不熟悉它的用法,你能提供一些启示吗?谢谢我使用Symfony,所以设置Guzzle&Goutte只是生成“composer require fabbot/Goutte”命令的一个例子。它需要某种类型的标记才能工作,但不需要ID或类,因此您可以获取标记或标记的内容。因此,它无法进行大小写匹配,如preg\u match
?我需要做一些事情,比如preg_match('website.com/page1&id='。$id)代码>搜索实际文本,然后找到它所包含的标记,然后移动到该标记的下一个子项。(因此,如果所需文本位于
中,则查找该标记的下一个匹配项,并获取其中的内容)
<?php
class Scrape
{
public $cookies = 'cookies.txt';
private $user = null;
private $pass = null;
/*Data generated from cURL*/
public $content = null;
public $response = null;
/* Links */
private $url = array(
'login' => 'https://website.com/login.jsp',
'submit' => 'https://website.com/LoginServlet',
'page1' => 'https://website.com/page1',
'page2' => 'https://website.com/page2',
'page3' => 'https://website.com/page3'
);
/* Fields */
public $data = array();
public function __construct ($user, $pass)
{
$this->user = $user;
$this->pass = $pass;
}
public function login()
{
$this->cURL($this->url['login']);
if($form = $this->getFormFields($this->content, 'login'))
{
$form['login'] = $this->user;
$form['password'] =$this->pass;
// echo "<pre>".print_r($form,true);exit;
$this->cURL($this->url['submit'], $form);
//echo $this->content;//exit;
}
//echo $this->content;//exit;
}
// NEW TESTING
public function loadPage($page)
{
$this->cURL($this->url[$page]);
echo $this->content;//exit;
}
/* Scan for form */
private function getFormFields($data, $id)
{
if (preg_match('/(<form.*?name=.?'.$id.'.*?<\/form>)/is', $data, $matches)) {
$inputs = $this->getInputs($matches[1]);
return $inputs;
} else {
return false;
}
}
/* Get Inputs in form */
private function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\']*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
/* Perform curl function to specific URL provided */
public function cURL($url, $post = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13");
// "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookies);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
if($post) //if post is needed
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
}
curl_setopt($ch, CURLOPT_URL, $url);
$this->content = curl_exec($ch);
$this->response = curl_getinfo( $ch );
$this->url['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
}
}
$sc = new Scrape('user','pass');
$sc->login();
$sc->loadPage('page1');
echo "<h1>TESTTESTEST</h1>";
$sc->loadPage('page2');
echo "<h1>TESTTESTEST</h1>";
$sc->loadPage('page3');
echo "<h1>TESTTESTEST</h1>";