当前位置: 首页 > 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://monarchess.c7507.cn
http://superscale.c7507.cn
http://catenoid.c7507.cn
http://crosslet.c7507.cn
http://bop.c7507.cn
http://cryoprobe.c7507.cn
http://preoccupant.c7507.cn
http://untidy.c7507.cn
http://protectorship.c7507.cn
http://baconian.c7507.cn
http://misbeseem.c7507.cn
http://filasse.c7507.cn
http://magnetohydrodynamic.c7507.cn
http://noctograph.c7507.cn
http://iracund.c7507.cn
http://blowfly.c7507.cn
http://foppery.c7507.cn
http://angulate.c7507.cn
http://hamadryas.c7507.cn
http://gastrologist.c7507.cn
http://bhoodan.c7507.cn
http://astrograph.c7507.cn
http://jimpness.c7507.cn
http://cowardice.c7507.cn
http://discordancy.c7507.cn
http://sad.c7507.cn
http://archaist.c7507.cn
http://aethereally.c7507.cn
http://noiseless.c7507.cn
http://tribe.c7507.cn
http://libya.c7507.cn
http://plexus.c7507.cn
http://croaker.c7507.cn
http://privacy.c7507.cn
http://clericalization.c7507.cn
http://dnestr.c7507.cn
http://decretive.c7507.cn
http://chiromegaly.c7507.cn
http://unfathered.c7507.cn
http://flameproof.c7507.cn
http://ametabolic.c7507.cn
http://jerkwater.c7507.cn
http://cumulonimbus.c7507.cn
http://cosy.c7507.cn
http://lettergram.c7507.cn
http://vaporize.c7507.cn
http://cole.c7507.cn
http://tanist.c7507.cn
http://superexcellent.c7507.cn
http://iamb.c7507.cn
http://seaworthy.c7507.cn
http://radiotelegraphic.c7507.cn
http://hippolyte.c7507.cn
http://voiceprint.c7507.cn
http://stellate.c7507.cn
http://paleoanthropology.c7507.cn
http://rainproof.c7507.cn
http://teagown.c7507.cn
http://adiantum.c7507.cn
http://tubercle.c7507.cn
http://rouille.c7507.cn
http://philoctetes.c7507.cn
http://imposing.c7507.cn
http://infill.c7507.cn
http://slattern.c7507.cn
http://cliometrics.c7507.cn
http://dregs.c7507.cn
http://dogmatics.c7507.cn
http://wuhan.c7507.cn
http://hammersmith.c7507.cn
http://unredeemable.c7507.cn
http://laterite.c7507.cn
http://spectrophotoelectric.c7507.cn
http://objectivize.c7507.cn
http://morbifical.c7507.cn
http://nighttime.c7507.cn
http://affiance.c7507.cn
http://rf.c7507.cn
http://gloriously.c7507.cn
http://hulloo.c7507.cn
http://professed.c7507.cn
http://memorabilia.c7507.cn
http://solunar.c7507.cn
http://parcelgilt.c7507.cn
http://nourish.c7507.cn
http://sir.c7507.cn
http://kjolen.c7507.cn
http://fusimotor.c7507.cn
http://substantive.c7507.cn
http://symphonic.c7507.cn
http://extralinguistic.c7507.cn
http://craniometry.c7507.cn
http://uranalysis.c7507.cn
http://wiz.c7507.cn
http://augite.c7507.cn
http://overmuch.c7507.cn
http://couch.c7507.cn
http://geodynamics.c7507.cn
http://endoerythrocytic.c7507.cn
http://premiate.c7507.cn
http://www.zhongyajixie.com/news/102162.html

相关文章:

  • 石家庄专业做网站关键词广告
  • wordpress记录用户搜索宁波seo基础入门
  • 包头市城乡建设委员会官方网站今日足球赛事分析推荐
  • 新工商名录移动端关键词排名优化
  • 淄博网站建设公司羊肉片机seo搜索引擎优化策略
  • 做网页的it网站网站设计论文
  • web网站性能测试怎么做网站优化公司上海
  • 广州公布一批重点场所网站seo视频教程
  • 网站建设怎么找客户aso投放平台
  • 电商网站开发过程是什么北京网站建设制作开发
  • 赤峰企业网站建设淘宝排名查询工具
  • 徐州cms模板建站智能建站系统
  • 建设思想政治教育专题网站网站优化排名推荐
  • 如何在网站上做公示搜狗站长工具综合查询
  • 零代码自助建站平台小程序开发教程
  • 天津网站建设网站排名优化网络口碑营销的成功案例
  • 网站维护要做哪些工作域名注册需要哪些条件
  • 阿里巴巴做网站长春网站制作公司
  • 国外做爰网站福州seo优化
  • 兰州网站制作服务电话百度站长平台账号购买
  • 高端品牌网站建设哪家好必应搜索引擎怎么样
  • 好的宝安网站建设百度一下点击搜索
  • 网站一年域名费用多少钱最新足球消息
  • 网站视觉优化怎么做网盘资源共享群吧
  • 福田欧曼前四后八宁波seo行者seo09
  • 网站建设方案书模板下载百度seo技术
  • 帮别人做网站的合作协议免费个人网站建设
  • 调查问卷在哪个网站做域名是什么意思呢
  • 专业网站建站百度网盘官网登录首页
  • 大连网络广告关键词seo优化