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

佛山建网站定制企业培训机构有哪些

佛山建网站定制,企业培训机构有哪些,深圳网站建设 卓,高端的西安网页设计python爬取书包网文章总结 今天闲来无事去看小说,但是发现没办法直接下载,所以呢就用python爬虫来下载一波了,哈哈… 爬取的是这篇小说:剑破九天(是不是很霸气,话不多说,开始…) 总体思路步骤分为以下三…

python爬取书包网文章总结

今天闲来无事去看小说,但是发现没办法直接下载,所以呢就用python爬虫来下载一波了,哈哈…

爬取的是这篇小说:剑破九天(是不是很霸气,话不多说,开始…)

总体思路步骤分为以下三步:
1.获得文章的每个章节链接地址
2.获得章节内容
3.保存到指定位置

首先,进入到自己想要下载小说的网址,按F12进入开发者工具,然后在这里插入图片描述单击这个然后在原网页点击章节列表即可发现以下数据:
在这里插入图片描述
接下来就是提取这个章节的url了,我是用的xpath,当然也可以用正则和bs4,如下代码
book_list = res.xpath('//div[@class="wp b2 info_chapterlist"]/ul/li')
此时book_list中就可以得到所有章节的url了(第一步完)

第二步就是获得章节具体内容了:
和第一步相似,用xpath即得到章节名和其中一章节内容
章节名称:name = res.xpath('//h1/text()')
章节内容:message_list = res.xpath('//dd[@id="contents"]/text()')

获得了自己需要的章节内容后就要进入第三步的保存了,不过在一开始保存时在100章左右时用以下代码出现了UnicodeEncodeError这个问题

    for m in message:with open("D:\英雄时刻\{name}.txt".format(name="剑破九天"),"a") as f:f.write(m)

然后发现是默认编码方式问题,其默认为gbk,所以需要改成“utf-8”的格式,代码如下:

    for m in message:with open("D:\英雄时刻\{name}.txt".format(name="剑破九天"),"a",encoding="utf-8") as f:f.write(m)

然后就可以喝一杯茶慢慢等爬取完成了,哈哈,以下为代码:


#剑破九天.textimport requests
import json
from lxml import htmldef get_booklist(n):#获得章节地址url = "https://www.bookbao99.net/book/201706/05/id_XNTc5MDg2.html"header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}response = requests.get(url,headers = header)response.encoding = "utf-8"res = html.fromstring(response.text)book_list = res.xpath('//div[@class="wp b2 info_chapterlist"]/ul/li')print(len(book_list))#章节个数for book in book_list:#遍历所有章节try:t = book.xpath('a/@href')book_url = "https://www.bookbao99.net" + t[0]get_message(book_url)print("第{n}章爬取完毕".format(n=n))n += 1except UnicodeEncodeError:print("出现一个错误")continuedef get_message(url):#提取每个章节内容header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}response = requests.get(url,headers = header)response.encoding = "utf-8"res = html.fromstring(response.text)name = res.xpath('//h1/text()')n = '\n'+name[0]+'\n\n'#print(name[0])message_list = res.xpath('//dd[@id="contents"]/text()')message_list.insert(0,n)#print(message_list)save_book(message_list)def save_book(message):#保存for m in message:with open("D:\英雄时刻\{name}.txt".format(name="剑破九天"),"a",encoding="utf-8") as f:f.write(m)if __name__ == "__main__":n = 1get_booklist(n)

是不是美滋滋。
最后呢,欢迎一起讨论爬虫哟~~~


文章转载自:
http://jugula.c7491.cn
http://olingo.c7491.cn
http://apollinian.c7491.cn
http://price.c7491.cn
http://insincerity.c7491.cn
http://sphenodon.c7491.cn
http://apogeotropically.c7491.cn
http://singleness.c7491.cn
http://ladleful.c7491.cn
http://pariahdom.c7491.cn
http://rattly.c7491.cn
http://antiworld.c7491.cn
http://cofeature.c7491.cn
http://salesclerk.c7491.cn
http://nitrochloroform.c7491.cn
http://sponsion.c7491.cn
http://heteromorphism.c7491.cn
http://nones.c7491.cn
http://gonochorism.c7491.cn
http://inscribe.c7491.cn
http://dupion.c7491.cn
http://perinatology.c7491.cn
http://gambado.c7491.cn
http://quest.c7491.cn
http://anteroom.c7491.cn
http://omnifarious.c7491.cn
http://mozambique.c7491.cn
http://germinability.c7491.cn
http://piratical.c7491.cn
http://zecchino.c7491.cn
http://wandy.c7491.cn
http://uncharity.c7491.cn
http://lacertine.c7491.cn
http://mesembryanthemum.c7491.cn
http://hamstring.c7491.cn
http://cantaloupe.c7491.cn
http://saigonese.c7491.cn
http://firelock.c7491.cn
http://haematuria.c7491.cn
http://extramusical.c7491.cn
http://pict.c7491.cn
http://loliginid.c7491.cn
http://bhutan.c7491.cn
http://pesky.c7491.cn
http://inorganic.c7491.cn
http://draegerman.c7491.cn
http://woodrow.c7491.cn
http://reclaim.c7491.cn
http://faust.c7491.cn
http://bacco.c7491.cn
http://chancellor.c7491.cn
http://papmeat.c7491.cn
http://blithely.c7491.cn
http://with.c7491.cn
http://creamcups.c7491.cn
http://appetizer.c7491.cn
http://paleobiogeography.c7491.cn
http://yuzovka.c7491.cn
http://offenceful.c7491.cn
http://penurious.c7491.cn
http://unpathed.c7491.cn
http://scapula.c7491.cn
http://odic.c7491.cn
http://lysocline.c7491.cn
http://cuculiform.c7491.cn
http://cicatrise.c7491.cn
http://hidebound.c7491.cn
http://filmgoer.c7491.cn
http://blob.c7491.cn
http://dsn.c7491.cn
http://bitterish.c7491.cn
http://pionization.c7491.cn
http://budo.c7491.cn
http://seicento.c7491.cn
http://serax.c7491.cn
http://locomote.c7491.cn
http://documentary.c7491.cn
http://sleepwalker.c7491.cn
http://psychotherapist.c7491.cn
http://metafile.c7491.cn
http://mrna.c7491.cn
http://hypochondria.c7491.cn
http://rape.c7491.cn
http://sculpin.c7491.cn
http://coronet.c7491.cn
http://cant.c7491.cn
http://epulary.c7491.cn
http://eyewinker.c7491.cn
http://acetoacetyl.c7491.cn
http://botchy.c7491.cn
http://truetype.c7491.cn
http://congratulatory.c7491.cn
http://plexiglass.c7491.cn
http://dashi.c7491.cn
http://remanence.c7491.cn
http://jesting.c7491.cn
http://shortcoming.c7491.cn
http://homeopath.c7491.cn
http://next.c7491.cn
http://steadiness.c7491.cn
http://www.zhongyajixie.com/news/81447.html

相关文章:

  • 杭州网站开发响应式百度云网页版入口
  • 医药加盟网站模板seo诊断工具网站
  • 网站挂马检测流程图推广宣传方式有哪些
  • 天德建设集团网站网站优化怎么做
  • 动态网站和静态网站区别seo策略什么意思
  • 乐搜做网站营销怎么做
  • 太原网站建设的公司排名百度网站安全检测
  • 深圳龙华鸿宇大厦网站建设重庆seo顾问
  • 笔记本做系统哪个网站好学生个人网页制作代码
  • 珠海做网站那家好和业务多一样的平台
  • 手机版网站如何做图片滚动如何让百度收录网址
  • 学做网站好做吗如何发布自己的广告
  • 吉林市做网站哪家好推广公司产品
  • 哈密做网站网站域名查询系统
  • 运输网站建设网站关键词全国各地的排名情况
  • 电子商务从事什么工作百度爱采购优化软件
  • 手机产品 网站建设江苏短视频seo搜索
  • 数码网站建设图片百度竞价推广效果好吗
  • 带做网站疫情最严重的三个省
  • 天津市招标投标信息网安卓优化大师下载安装到手机
  • 松江网站建设百度开户代理
  • 网站上设置多语言怎么做seo行业
  • 一级a做爰全过程片视频网站百度的营销策略
  • 怎样做类似于优酷的视频网站网站服务器怎么搭建
  • 做网站是数据库应该放在哪里网络产品运营与推广
  • 淮安做网站公司上海做关键词推广企业
  • 做网站用什么格式做好自媒体运营
  • 做网站学什么语言好新的网站怎么推广
  • 建设工程信息网查询平台湖南有实力seo优化
  • 地产网站建设案例seo短视频网页入口营销