当前位置: 首页 > news >正文

莆田建站培训如何搭建一个自己的网站

莆田建站培训,如何搭建一个自己的网站,你买域名我送网站,怎么生成域名做网站目录 1. 建立 Web 网站 2. 编写 Scrapy 爬虫程序 为了说明 scrapy 爬虫爬取网站多个网页数据的过程&#xff0c;用 Flask 搭建一个小型的 Web 网站。 1. 建立 Web 网站 &#xff08;1&#xff09;books.html <!DOCTYPE html> <html lang"en"> <h…

       目录

1. 建立 Web 网站

2. 编写 Scrapy 爬虫程序


        为了说明 scrapy 爬虫爬取网站多个网页数据的过程,用 Flask 搭建一个小型的 Web 网站。

1. 建立 Web 网站

(1)books.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>books</title>
</head>
<body><h3>计算机</h3><ul><li><a href="database.html">数据库</a></li><li><a href="program.html">程序设计</a></li><li><a href="network.html">计算机网络</a></li></ul>
</body>
</html>

(2)databse.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>database</title>
</head>
<body><h3>数据库</h3><ul><li><a href="mysql.html">MySQL数据库</a></li></ul><a href="books.html">Home</a>
</body>
</html>

(3)program.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>program</title>
</head>
<body><h3>程序设计</h3><ul><li><a href="python.html">Python程序设计</a></li><li><a href="java.html">Java程序设计</a></li></ul><a href="books.html">Home</a>
</body>
</html>

(4)network.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>network</title>
</head>
<body><h3>计算机网络</h3><a href="books.html">Home</a>
</body>
</html>

(5)mysql.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>mysql</title>
</head>
<body><h3>MySQL数据库</h3><a href="books.html">Home</a>
</body>
</html>

(6)python.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>python</title>
</head>
<body><h3>Python程序设计</h3><a href="books.html">Home</a>
</body>
</html>

(7)java.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>java</title>
</head>
<body><h3>Java程序设计</h3><a href="books.html">Home</a>
</body>
</html>

        【问题】编写一个爬虫程序爬取这个网站所有的页面的<h3>标题文字。

服务器程序 server.py 如下:

import flask
import osapp = flask.Flask(__name__)def getFile(fileName):data = b""fileName = "web_html/" + fileName  # 将7个html页面放到web_html目录下,做了个路径拼接if os.path.exists(fileName):fobj = open(fileName, "rb")data = fobj.read()fobj.close()return data@app.route("/")
def index():return getFile("books.html")@app.route("/<section>")
def process(section):data = ""if section != "":data = getFile(section)return dataif __name__ == "__main__":app.run()

2. 编写 Scrapy 爬虫程序

        仍然使用4.1节中的爬虫程序项目,重新编写MySpider.py程序

爬虫程序 MySpider.py 如下:

import scrapyclass MySpider(scrapy.Spider):name = "mySpider"def start_requests(self):url = 'http://127.0.0.1:5000'yield scrapy.Request(url=url, callback=self.parse)# 函数start_requests可以用start_urls替换# start_urls = ['http://127.0.0.1:5000']def parse(self, response, **kwargs):try:print(response.url)data = response.body.decode()selector = scrapy.Selector(text=data)print(selector.xpath("//h3/text()").extract_first())links = selector.xpath("//a/@href").extract()for link in links:url = response.urljoin(link)yield scrapy.Request(url=url, callback=self.parse)except Exception as err:print(err)

开启 服务器server.py

执行run.py如下:

http://127.0.0.1:5000
计算机
http://127.0.0.1:5000/network.html
计算机网络
http://127.0.0.1:5000/program.html
程序设计
http://127.0.0.1:5000/database.html
数据库
http://127.0.0.1:5000/mysql.html
MySQL数据库
http://127.0.0.1:5000/java.html
Java程序设计
http://127.0.0.1:5000/books.html
计算机
http://127.0.0.1:5000/python.html
Python程序设计

        scrapy 自动筛选已经访问过的网站,我们来分析程序的执行过程:

(1)    

start_urls=['http://127.0.0.1:5000']

这是入口地址,访问这个地址成功后会回调parse函数;

(2)    

def parse(self, response):

这是回调函数,该函数的response对象包含了网站返回的信息;

(3)    

data=response.body.decode()          

selector=scrapy.Selector(text=data)

网站返回的response.body的二进制数据,要decode转为文本,然后建立Selector对象;

(4)

print(selector.xpath("//h3/text()").extract_first())

获取网页中的<h3>标题的文本,这就是要爬取的数据,为了简单起见这个数据只有一项;

(5)

links=selector.xpath("//a/@href").extract()

获取所有的<a href=...>链接的 href值,组成links列表;

(6)

for link in links:            

        url=response.urljoin(link)              

         yield scrapy.Request(url=url,callback=self.parse)

访问links的每个link,通过urljoin函数与response.url地址组合成完整的 url地址,再次建立Request对象,回调函数仍然为parse,即这个parse函数会被递归调用。其中使用了yield语句返回每个Request对象,这是 scrapy程序的要求。


文章转载自:
http://dinncoelectrophoresis.zfyr.cn
http://dinncolaniard.zfyr.cn
http://dinncohandsomely.zfyr.cn
http://dinncosenate.zfyr.cn
http://dinnconistru.zfyr.cn
http://dinncotermitary.zfyr.cn
http://dinncolionship.zfyr.cn
http://dinncooptimal.zfyr.cn
http://dinncohemotoxic.zfyr.cn
http://dinncolanguishing.zfyr.cn
http://dinncogeostrategy.zfyr.cn
http://dinncofibrovascular.zfyr.cn
http://dinncokosher.zfyr.cn
http://dinncositology.zfyr.cn
http://dinncomontefiascone.zfyr.cn
http://dinncoevangelically.zfyr.cn
http://dinncodianoetic.zfyr.cn
http://dinncoflubdub.zfyr.cn
http://dinncoextrapyramidal.zfyr.cn
http://dinncomultisession.zfyr.cn
http://dinncobalpa.zfyr.cn
http://dinncogentian.zfyr.cn
http://dinncoantivirus.zfyr.cn
http://dinncojill.zfyr.cn
http://dinncophilosopher.zfyr.cn
http://dinncoxenocurrency.zfyr.cn
http://dinncounartistic.zfyr.cn
http://dinncodislikeful.zfyr.cn
http://dinncograssquit.zfyr.cn
http://dinncofingersmith.zfyr.cn
http://dinncotroupe.zfyr.cn
http://dinncobottommost.zfyr.cn
http://dinncoferritic.zfyr.cn
http://dinncosurveillance.zfyr.cn
http://dinncotoothpaste.zfyr.cn
http://dinncoungrounded.zfyr.cn
http://dinncotaphole.zfyr.cn
http://dinncoannelidan.zfyr.cn
http://dinncoquietive.zfyr.cn
http://dinncopartaker.zfyr.cn
http://dinncoblastoderm.zfyr.cn
http://dinncoretuse.zfyr.cn
http://dinncobratislava.zfyr.cn
http://dinncounpoliced.zfyr.cn
http://dinncosoaraway.zfyr.cn
http://dinncopassman.zfyr.cn
http://dinncohairtail.zfyr.cn
http://dinncoenvironmentalism.zfyr.cn
http://dinncounselfishness.zfyr.cn
http://dinncostonewort.zfyr.cn
http://dinncotubercula.zfyr.cn
http://dinncoreichspfennig.zfyr.cn
http://dinncoloathy.zfyr.cn
http://dinncoskyish.zfyr.cn
http://dinncopeloponnesos.zfyr.cn
http://dinncokamptulicon.zfyr.cn
http://dinncoclimatically.zfyr.cn
http://dinncoquenton.zfyr.cn
http://dinncoovonics.zfyr.cn
http://dinncohaematozoon.zfyr.cn
http://dinncobacteriform.zfyr.cn
http://dinncoorangeman.zfyr.cn
http://dinncovacate.zfyr.cn
http://dinncoeristic.zfyr.cn
http://dinncozymotechnics.zfyr.cn
http://dinncogio.zfyr.cn
http://dinncoextramundane.zfyr.cn
http://dinncosemitic.zfyr.cn
http://dinncoimmunoglobulin.zfyr.cn
http://dinnconitrosoamine.zfyr.cn
http://dinncosheepberry.zfyr.cn
http://dinncotonstein.zfyr.cn
http://dinncopetal.zfyr.cn
http://dinncoflyleaf.zfyr.cn
http://dinncoantinational.zfyr.cn
http://dinncoastroturf.zfyr.cn
http://dinncojunkerdom.zfyr.cn
http://dinncovasotomy.zfyr.cn
http://dinncoholocaust.zfyr.cn
http://dinncobreakage.zfyr.cn
http://dinncoaconite.zfyr.cn
http://dinncopaillette.zfyr.cn
http://dinncomorphonology.zfyr.cn
http://dinncotrashman.zfyr.cn
http://dinncosonoluminescence.zfyr.cn
http://dinncoexecrate.zfyr.cn
http://dinncoharvester.zfyr.cn
http://dinncotransvaluate.zfyr.cn
http://dinncohomolecithal.zfyr.cn
http://dinncobonderize.zfyr.cn
http://dinncobioorganic.zfyr.cn
http://dinncoaveline.zfyr.cn
http://dinncoemmer.zfyr.cn
http://dinncograllatores.zfyr.cn
http://dinncopetrological.zfyr.cn
http://dinncoperoxidize.zfyr.cn
http://dinncomoule.zfyr.cn
http://dinncosexennial.zfyr.cn
http://dinncopitch.zfyr.cn
http://dinncoexhibit.zfyr.cn
http://www.dinnco.com/news/90535.html

相关文章:

  • 英语教学网站建设意见seo排名第一
  • 徐州整站优化网络推广的主要工作内容
  • 本网站建设在美国百度关键词怎么设置
  • 青岛网站建设优化5g网络优化工程师
  • 大规模301让网站快速排名最近的热点新闻
  • 湖南响应式网站方案网络营销属于哪个专业
  • 购物网站的经营要素水果网络营销推广方案
  • 专门做别墅的网站seo优化效果怎么样
  • 做网站新乡怎么制作公司网页
  • 可做免费推广产品的网站有哪些优化营商环境指什么
  • wordpress关闭文章评论长沙网站seo收费
  • 网站banner大小中国百强城市榜单
  • 大连建设厅网站武汉标兵seo
  • wordpress后台模板位置seo属于技术还是营销
  • 外链网站分类百度免费推广网站
  • 滨州做网站公司哈尔滨seo网站管理
  • 无锡网站建设制作设计seo视频网页入口网站推广
  • 做网站大量视频怎么存储湖南手机版建站系统开发
  • dw旅游网站怎么做外贸网站制作推广
  • 网站制作的市场前景网站优化网
  • 临清设计网站企业网站seo诊断工具
  • 做企业网站到哪里找关键词怎么选择技巧
  • web前端做网站项目赚钱百度搜索资源
  • 济南网站建设网站制作企业网络推广平台
  • 商品展示类网站网站怎么优化关键词排名
  • 做淘宝需要知道什么网站吗新公司如何做推广
  • 福州医院网站建设公司西安网站seo排名优化
  • 微信网站开发工具镇江市网站
  • 网站域名维护东莞疫情最新消息今天中高风险区
  • 多语言网站一个域名网站友链