php抓取网页动态数据(什么是动态网站?动态友好,搜索引擎容易识别(一) )

优采云 发布时间: 2022-03-21 03:29

  php抓取网页动态数据(什么是动态网站?动态友好,搜索引擎容易识别(一)

)

  什么是LNMP

  LNMP 是指一组通常一起用于运行动态网站 或服务器的免费软件名称首字母缩略词。

  即Linux+Nginx+Mysql+PHP网站服务器架构

  这四种软件都是免费开源的软件,结合在一起,就成了一个免费、高效、可扩展性强的网站服务系统。

  当前的网站一般有动态和静态数据。默认情况下,nginx 只能处理静态数据。用户访问任何数据都会直接返回相应的文件。如果访问的是脚本,则直接返回一个脚本给用户,而用户没有脚本解释器,无法读取脚本源码!如图所示。

  

  所以需要集成LNMP(Linux、Nginx、MySQL、PHP)来实现动态的网站效果。

  在 CentOS 系统中,LNMP 从源代码安装 Nginx,并使用 RPM 包安装 MariaDB、PHP 和 PHP-FPM 软件。

  什么是静态网页?

  在网站的设计中,纯HTML(标准通用标记语言下的应用)格式的网页通常被称为“静态网页”。静态网页是一个标准的 HTML 文件,文件扩展名为 .htm、.html,可以收录文本、图像、声音、FLASH *敏*感*词*、客户端脚本、ActiveX 控件和 JAVA 小程序等。

  静态数据:图片音频视频

  静态网页使用语言:HTML

  静态网页的特点

  可以公开(即可以向任何人证明副本)。

  托管没有任何特殊要求。(无需超文本预处理器、公共网关接口等特殊中间件)

  没有网络服务器或应用服务器,例如直接从 CD-ROM(激光光盘 - 只读存储器)或 USB 闪存驱动器读取内容,可以直接通过网络浏览器访问。

  网站更安全,HTML页面不会受到Asp相关漏洞的影响;它可以减少攻击并防止SQL注入。当数据库发生错误时,不会影响网站的正常访问。

  无需编译,速度快,节省服务器资源。

  URL 格式友好且易于被搜索引擎识别。

  什么是动态网站?

  动态网站不是指具有*敏*感*词*功能的网站,而是指可以根据不同情况动态改变内容的网站。一般来说,动态 网站 Schema 通过数据库。动态网站除了设计网页,还通过数据库和编程让网站拥有更多自动化和高级的功能。动态网站一般体现在使用asp、jsp、php、aspx等技术的网页中。

  也可以简单理解为是用脚本写的

  即动态网页是指收录程序代码的网页文件,通过后台数据库与Web服务器的信息进行交互。

  后台数据库提供实时数据更新和数据查询服务。

  例如:shell PHP Java Python ...

  动态网页使用的语言:HTML+ASP或HTML+PHP或HTML+JSP等。

  动态网页功能:

  Dynamic网站可实现交互功能,如用户注册、信息发布、产品展示、订单管理等;

  动态网页不是独立存在于服务器上的网页文件,只是在浏览器发出请求时才返回网页;

  动态网页收录服务器端脚本,因此页面文件名通常以asp、jsp、php等为后缀。但您也可以使用URL静态技术使网页后缀显示为HTML。因此,不能以页面文件的后缀作为判断网站动静态的唯一标准。

  由于动态网页需要数据库处理,动态网站的访问速度大大减慢;

  由于特殊代码的存在,动态网页对搜索引擎的友好性不如静态网页。

  但是随着电脑性能的提高和网络带宽的提高,后两者已经基本解决了。

  动态网页和静态网页的区别

  1、网页制作中使用的制作语言:

  静态网页使用的语言:超文本标记语言(标准通用标记语言的一种应用)

  动态网页使用语言:超文本标记语言+ASP或超文本标记语言+PHP或超文本标记语言+JSP等。

  2、程序是否在服务器端运行是一个重要的标志。

  在服务器端运行的程序、网页和组件都属于动态网页。它们会在不同的客户端和不同的时间返回不同的网页,例如 ASP、PHP、JSP、ASPnet、CGI 等。运行在客户端的程序、网页、插件、组件属于静态网页,如html页面、Flash、JavaScript、VBScript等,永远不变。

  四种常见的动态 Web 技术部署 LNMP 环境

  已安装软件列表如下:

  软件包的作用:

mariadb(数据库客户端软件)、

mariadb-server(数据库服务器软件)、

mariadb-devel(其他客户端软件的依赖包)、

php(解释器)、

php-fpm(进程管理器服务)、

php-mysql(PHP的数据库扩展包)。

  1)使用yum安装基础依赖(如果已经安装依赖,忽略此步骤)

  [root@proxy ~]# yum -y install gcc openssl-devel pcre-devel

  2)从源码安装Nginx(如果已经安装了Nginx,忽略这一步)

  [root@proxy ~]# useradd -s /sbin/nologin nginx

[root@proxy ~]# tar -xvf nginx-1.12.2.tar.gz

[root@proxy ~]# cd nginx-1.12.2

[root@proxy nginx-1.12.2]# ./configure \

> --user=nginx --group=nginx \

> --with-http_ssl_module

[root@proxy ~]# make && make install

  3)安装 MariaDB

  root@proxy ~]# yum -y install mariadb mariadb-server mariadb-devel

  4)php 和 php-fpm php-mysql

  [root@proxy ~]# yum -y install php php-mysql

[root@proxy ~]# yum -y install php-fpm

  5)启动Nginx服务(如果已经启动了nginx,可以忽略这一步)

  这里需要注意的是,如果服务器上已经启动了其他*敏*感*词*80端口的服务软件(如httpd),则需要先关闭该服务,否则会发生冲突。

  [root@proxy ~]# systemctl stop httpd ##如果该服务存在则关闭该服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx ##启动Nginx服务

[root@proxy ~]# netstat -utnlp | grep :80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32428/nginx

  6)启动 MySQL 服务

  [root@proxy ~]# systemctl start mariadb //启动服务器

[root@proxy ~]# systemctl status mariadb //查看服务状态

[root@proxy ~]# systemctl enable mariadb //设置开机启动

  7)启动PHP-FPM服务

  [root@proxy ~]# systemctl start php-fpm //启动服务

[root@proxy ~]# systemctl status php-fpm //查看服务状态

[root@proxy ~]# systemctl enable php-fpm //设置开机启动

  这样一个LNMP环境就准备好了

  配置nginx+PHP实现两个if

  localtion (匹配用户的地址拦,支持正则,从域名后面开始匹配)

server {

listen 80;

server_name www.a.com

location /test { dney 1.1;allow all}

location /qq {allow 2.2;deny all}

location / {allow all} (##优先级最低,跟顺序无关,动静匹配)

}

firefox http://www.a.com/qq ##结果[2.2打开,不是则打不开]

firefox http://www.a.com/test ##结果[只要不是1.1就能打开]

firefox http://www.a.com/ ##匹配第三个localtion

firefox http://www.a.com/nb ##匹配第三个localtion

firefox http://www.a.com/xyz ##匹配第三个localtion

firefox http://www.a.com/123 ##匹配第三个localtion

server { ##匹配静态的网站

listen 80;

server_name www.a.com

location / {

root html;

}

location ~ \.php$ { ##匹配动态的网站

root heml;

fastcgi_pass 127.0.0.1:9000; (转发给PHP翻译)

}

}

firefox http://www.a.com/a.jpg ##只要不是以.php结尾,都是静态处理

firefox http://www.a.com/test.php ##动态处理,显示动态页面

  什么是动静分离?FastCGI 介绍 FastCGI 工作 FastCGI 的缺点:

  高内存消耗

  因为是多进程,所以比CGI多线程消耗更多的服务器内存

  PHP-CGI 解释器每个进程消耗 7 到 25 兆内存,将此数字乘以 50 或 100 是大量内存

  nginx+PHP(FastCGI)服务器在30000个并发连接下开启10个nginx进程,消耗150M内存(1015M)

  开启64个php_cgi进程消耗1280M内存(20M64)

  部署 Nginx+FastCGI

  通过调整 Nginx 服务器配置,达到以下目标:

  下面使用两台虚拟机,一台作为LNMP服务器(192.168.4.5)。另一台作为测试客户端(192.168.4.10),如图所示。

  

  Nginx结合FastCGI技术可以支持PHP页面架构,如图。

  

  1.修改配置文件

  [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

root html;

index index.php index.html index.htm;

#设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户

}

location ~ \.php$ {

root html;

fastcgi_pass 127.0.0.1:9000; #将请求转发给本机9000端口,PHP解释器

fastcgi_index index.php;

#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi.conf; #加载其他配置文件

}

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:

#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

注:/usr/local/nginx/conf这个配置文件目录下nginx.conf.default 这个配置文件可以帮助我们还原配置文件

用法:

[root@proxy ~]# cd /usr/local/nginx/conf/

[root@proxy ~]# cp nginx.conf.default nginx.conf

  2.创建PHP测试页面,测试LNMP架构能否解析PHP页面

  [root@proxy ~]# vim /usr/local/nginx/html/test.php

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

[root@proxy conf]# curl localhost/test.php

33

  3.创建一个PHP测试页面,连接并查询MariaDB数据库。

  可以参考lnmp_soft/php_scripts/mysql.php:

  [root@proxy ~]# vim /usr/local/nginx/html/mysql.php

query($sql);

while($row = $result->fetch_array()){

printf("Host:%s",$row[0]);

printf("</br>");

printf("Name:%s",$row[1]);

printf("</br>");

}

?>

[root@proxy html]# firefox 192.168.4.5/mysql.php

  访问结果如下:

  

  [root@proxy html]# mysql -e "grant all on *.* to dc@localhost identified by &#39;123&#39;" ##给数据库添加一个用户

[root@proxy html]# firefox 192.168.4.5/mysql.php

  访问结果如下:

  

  动态网页会实时更新显示内容

  4)LNMP 常见问题解答

  Nginx 高级技术地址改写 什么是地址改写?地址重写重写语法的好处

   - rewrite 基本语句

- rewrite regex replacement flag

- rewrite 旧地址 新地址 [选项]

- flag: break, last, redirect, permanent

- last: 停止执行其他重写规则,根据URI继续搜索其他location,地址栏不变(不再读其他rewrite)

- break: 停止执行其他的重写规则,完成本次请求(不再读其他语句,结束请求 location语句)

- redirect: 302临时重定向.地址栏改变,爬虫不更新

- permanent: 301永久重定向,地址栏改变,爬虫更新

- if (条件){...}

  正则表达式

  按照上面的例子,通过调整Nginx服务器配置,可以达到以下目标:

  所有访问a.html的请求,重定向到b.html;

  所有对 192.168.4.5 的请求都重定向到;

  所有对192.168.4.5/以下子页面的访问都会被重定向到下面的同一页面;

  实现firefox和curl访问同一个页面文件,返回不同的内容。

  访问 a.html 重定向到 b.html

  [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

35 server {

36 listen 80;

37 server_name localhost;

38 rewrite /a.html /b.html; ##(地址重写,访问a.html重定向到b.html)

[root@proxy conf]# echo "BBB" > /usr/local/nginx/html/b.html ##创建b网页

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

[root@proxy conf]# curl 192.168.4.5/a.html ##访问a.html的测试结果

BBB

[root@proxy conf]# firefox 192.168.4.5/a.html ##访问a.html的测试结果

  测试结果:

  

  [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

35 server {

36 listen 80;

37 server_name localhost;

38 rewrite /a.html /b.html redirect;

[root@proxy conf]# curl 192.168.4.5/a.html ##访问a.html的测试结果

BBB

[root@proxy conf]# firefox 192.168.4.5/a.html ##访问a.html的测试结果

  测试结果;

  

  访问 192.168.4.5 的请求重定向到

  [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

35 server {

36 listen 80;

37 server_name localhost;

38 rewrite ^/ http://www.tmooc.cn;

[root@proxy conf]# /usr/local/nginx/sbin/nginx -s reload

[student@room9pc01 ~]$ firefox 192.168.4.5 ##用真机测试

  测试结果如图:

  

  [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

35 server {

36 listen 80;

37 server_name localhost;

38 rewrite ^/(.*) http://www.tmooc.cn/$1;

[root@proxy conf]# /usr/local/nginx/sbin/nginx -s reload

[student@room9pc01 ~]$ firefox 192.168.4.5

[student@room9pc01 ~]$ firefox 192.168.4.5/free ##用真机测试

  访问 192.168.4.5 个结果:

  

  访问 192.168.4.5/free 的结果:

  

  实现firefox和curl访问同一个页面文件,返回不同的内容

  思路:

浏览器 : curl firefox

①:/usr/local/nginx/html/test.html 网页根目录: html

②:/usr/local/nginx/html/firefox/test.html 网页根目录下的子目录: firefox

>curl http://192.168.4.5/test.html

>firefox http://192.168.4.5/test.html

两个浏览器:访问相同(结果为第①个)

假设用rewrite处理:

>rewrite /test.html /firefox/test.html;

>curl http://192.168.4.5/test.html

>firefox http://192.168.4.5/test.html

两个浏览器访问相同(结果为第②个)

假设使用if判断语句配和rewrite地址重写处理:

>if(如果用户的浏览器是火狐){....}

>rewrite /test.html /firefox/test.html;

>curl http://192.168.4.5/test.html 访问网页根目录

>firefox http://192.168.4.5/test.html 访问/firefox/test.html

  [root@proxy logs]# mkdir -p /usr/local/nginx/html/firefox

[root@proxy logs]# echo "I am tian" > /usr/local/nginx/html/test.html

[root@proxy logs]# echo "firefox tian" > /usr/local/nginx/html/firefox/test.html

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

35 server {

36 listen 80;

37 server_name localhost;

38 if ($http_user_agent ~* firefox) {

39 rewrite ^/(.*) /firefox/$1;

40 }

##$http_user_agent :存着用户信息和操作系统的信息

[root@proxy conf]# /usr/local/nginx/sbin/nginx -s reload

[student@room9pc01 ~]$ curl 192.168.4.5/test.html

I am tian

[student@room9pc01 ~]$ firefox 192.168.4.5/test.html

  火狐的结果:

  

  使用 break、last、redirect、permanent

  以下为简要说明,仅供参考

  rewrite /a.html /b.html;

rewtite /b.html /c.html;

访问a.html打开c.html

rewrite /a.html /b.html last;

rewtite /b.html /c.html;

访问a.html打开b.htlm

location /a.html {

rewrite /a.html /b.html;

}

location /b.html {

rewrite /b.html c.html;

}

访问a.html打开c.html

location /a.html {

rewrite /a.html /b.html last;

}

location /b.html {

rewrite /b.html c.html;

}

访问a.html打开c.html

location /a.html {

rewrite /a.html /b.html break;

}

location /b.html {

rewrite /b.html c.html;

}

访问a.html打开b.html

redirect 临时重定向,一般用于a.html临时有问题且能恢复的情况下

permanent 永久重定向,一般用于a.html永久不能访问,但是为了方便客户端访问而用

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线