php爬虫获取数据库 php数据抓取

如何用php获取数据库信息并显示获取ppq数据库php爬虫获取数据库的所有表名的代码:
?php
$server='localhost';
$user='root';
$pass='12345';
$dbname='ppq';
$conn=mysql_connect($server,$user,$pass);
if(!$conn)
die("数据库系统连接失败php爬虫获取数据库!");
$result=mysql_list_tables($dbname);
if(!$result)
die("数据库连接失败!");
while($row=mysql_fetch_row($result))
{
echo
$row[0]."
";
}
mysql_free_result($result);
?
mysql_list_tables
(PHP
3,
PHP
4
,
PHP
5)
mysql_list_tables
--
列出
MySQL
数据库中的表
说明
resource
mysql_list_tables
(
string
database
[,
resource
link_identifier])
mysql_list_tables()
接受一个数据库名并返回和
mysql_query()
函数很相似的一个结果指针 。用
mysql_fetch_array()或者用mysql_fetch_row()来获得一个数组php爬虫获取数据库,数组的第0列就是数组名,当获取不到时
mysql_fetch_array()或者用mysql_fetch_row()返回
FALSE 。
php读取数据库信息的几种方法连接到一个 url 地址为localhost 、 端口为 3306 的mysql服务器上 。mysql服务器的帐号是"root",密码是"9999" 。mysql 服务器上有一个数据库 ok,数据库里有一个表 abc 。表 abc 一共为两列,列名分别是 "id" 和 "name" ,将 abc 里的所有数据读出来 。
?
$dbh = @mysql_connect("localhost:3306","root","9999");
/* 定义变量dbh , mysql_connect()函数的意思是连接mysql数据库, "@"的意思是屏蔽报错 */
if(!$dbh){die("error");}
/* die()函数的意思是将括号里的字串送到浏览器并中断PHP程式 (Script) 。括号里的参数为欲送出的字串 。*/
@mysql_select_db("ok", $dbh);
/* 选择mysql服务器里的一个数据库,这里选的数据库名为 ok */
$q = "SELECT * FROM abc";
/* 定义变量q, "SELECT * FROM abc"是一个SQL语句,意思是读取表abc中的数据 */
?
br /
!--========= 方法一 =========--
br /
?
$rs = mysql_query($q, $dbh);
/* 定义变量 rs ,函数mysql_query()的意思是:送出 query 字串供 MySQL 做相关的处理或者执行.由于php是从右往左执行的,所以,rs的值是服务器运行mysql_query()函数后返回的值 */
if(!$rs){die("Valid result!");}
echo "table";
echo "trtdID/tdtdName/td/tr";
while($row = mysql_fetch_row($rs)) echo "trtd$row[0]/tdtd$row[1]/td/tr";
/* 定义量变(数组)row,并利用while循环,把数据一一写出来.
函数mysql_fetch_row()的意思是:将查询结果$rs单列拆到阵列变数中.
$row[0] 和 $row[1] 的位置可以换*/
echo "/table";
?
br /
!--========= 方法二 =========--
br /
?
$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_object($rs)) echo "$row-id $row-name br /";
/* id和name可以换位置 */
?
br /
!--========= 方法三 =========--
br /
?
$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_array($rs)) echo "$row[id] $row[name] br /";
/* id和name可以换位置 */
?
!--========= 方法三最快 =========--
?
@mysql_close($dbh);
/* 关闭到mysql数据库的连接 */
?
php中curl爬虫 怎么样通过网页获取所有链接本文承接上面两篇,本篇中的示例要调用到前两篇中的函数,做一个简单的URL采集 。一般php采集网络数据会用file_get_contents、file和cURL 。不过据说cURL会比file_get_contents、file更快更专业 , 更适合采集 。今天就试试用cURL来获取网页上的所有链接 。示例如下:
?php
/*
* 使用curl 采集hao123.com下的所有链接 。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 页面内容我们并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回结果,而不是输出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主机部分,补全用
$host = '';
if (is_array($linkarr)) {
foreach ($linkarr as $k = $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("p此页面的所有链接为:/ppre%s/pren", var_export($linkresult , true));
?
function.php内容如下(即为上两篇中两个函数的合集):
?php
function _striplinks($document) {
preg_match_all("'s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s] ))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input:$linksthe links to qualify
$URIthe full URI to get the base from
Output:$expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?] /",$URI,$match);
$match = preg_replace("|/[^/.] .[^/.] $|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array("|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/] /../|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?
php怎么获得mysql数据库的数据首先你要看php.ini有没有开启mysql的拓展函数库 , 然后mysql_connect()连接数据库,mysql_query("set names utf8");设置编码格式 , 然后mysql_select_db()设置查询的数据库
mysql_query()执行sql语句,mysql_fetch_array()或者mysql_fetch_assoc()或者mysql_fetch_num()获取结果集,mysql_close()最后关闭数据库连接,明白了么
【php爬虫获取数据库 php数据抓取】关于php爬虫获取数据库和php数据抓取的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读