Python爬取分析全网最硬核粽子(附源码)

说到粽子,想必大家都是会想起这些普通的粽子,即使再有南北差异大家也见怪不怪了

但有种硬核粽子的味道在行哥的记忆里一直不能忘怀,那就是《盗墓笔记》里的粽子。这种粽子最好需要黑驴蹄子来搭配食用更加美味哦

本文行哥爬取了整本的《盗墓笔记》来分析一下粽子的口味到底有多奇特

1.代码爬取

本文将通过小说网站http://www.daomubiji.com/来爬取整本盗墓笔记并保存,在这一过程中使用python网络库requests实现简单的python爬虫以及使用html文档分析库BeautifulSoup分析

网页爬取代码如下,可以复制粘贴直接运行

# 公众号:一行数据

from bs4 import BeautifulSoup

import requests

import re

# 获取每本书的链接

def get_book_urls(url):

book_urls = []

index = requests.get(“http://www.daomubiji.com/”)

soup = BeautifulSoup(index.content.decode(“utf8”), ‘lxml’)

articles = soup.find_all(“article”, class_=‘article-content’)

for article in articles:

links = article.find_all(‘a’, href=re.compile(“dao-mu-bi-ji”))

for link in links:

book_urls.append(link[“href”])

return book_urls

# 获取每章的链接

def get_chapter_urls(url):

chapter_urls = []

page = requests.get(url)

soup = BeautifulSoup(page.content.decode(“utf8”), “lxml”)

articles = soup.find_all(“article”, class_=“excerpt excerpt-c3”)

for article in articles:

chapter_urls.append(article.a[“href”])

return chapter_urls

# 获取每章的内容

def get_content(url):

content = “”

page = requests.get(url)

soup = BeautifulSoup(page.content.decode(“utf8”), “lxml”)

title = soup.find_all(“h1”, class_=“article-title”)[0].string

content += (“\n” + title + “\n\n”)

articles = soup.find_all(“article”, class_=“article-content”)

for article in articles:

ps = article.find_all(‘p’)

for p in ps:

for string in p.strings:

content = content + string + “\n”

return content

# 获取全本《盗墓笔记》并保存到文件

def get_article(url):

print(1)

book_urls = get_book_urls(url)

print(1)

chapter_urls = []

for url in book_urls:

print(1)

# url = “http://www.daomubiji.com/dao-mu-bi-ji-2”

chapter_urls.extend(get_chapter_urls(url))

print(chapter_urls)

result = “”

for chapter_url in chapter_urls:

content = get_content(chapter_url)

result += content

print(content)

with open(“daomubiji.txt”, “a”) as f:

f.write(result.encode(“utf8”))

get_article(“http://www.daomubiji.com/”)

2.粽子分析

依稀记得书中的粽子种类繁多

  • 大粽子:厉害的僵尸、恶鬼之类的东西

  • 老粽子:可发生尸变的不好对付的僵尸

  • 干粽子:是指墓里的尸体烂得只剩下一堆白骨了。

现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。

分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值