首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >爬虫学习(12):爬取诗词名句网并且下载保存

爬虫学习(12):爬取诗词名句网并且下载保存

作者头像
川川菜鸟
发布2021-10-18 16:13:29
发布2021-10-18 16:13:29
5760
举报

用BeautifulSoup爬取并且下载。仅仅用作学习用途哈,不然又侵权了。 效果:

由于我是正在自学爬虫,不是很能找到非常优化的办法,是一名计算机大二学生,代码可能不是很好,还请大神指点,这是我扣扣群:970353786,希望更多喜欢学习python的可以跟我一起学习交流。 上代码:

代码语言:javascript
复制
import requests
from bs4 import BeautifulSoup
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 Edg/86.0.622.63'
}
url = 'https://www.shicimingju.com/book/hongloumeng.html'
page_text = requests.get(url=url,headers=headers).content.decode('utf-8')
soup = BeautifulSoup(page_text,'lxml')
mulu=soup.find_all(attrs={'class':'book-mulu'})
# mulu=soup.select('.book-mulu')
# print(mulu)
fp = open('./论语.txt','w',encoding='utf-8')
for ul in mulu:
    a=ul.find_all(name='a')
    for i in a:
        title = i.string
        new_url = 'https://www.shicimingju.com' + i['href']
        # print(new_url)
        # print(title)
        html=requests.get(url=new_url,headers=headers).content.decode('utf-8')
        new_soup=BeautifulSoup(html,'lxml')
        # print(soup)
        for  wenben in new_soup.find_all('div',{'class':'chapter_content'}):
            print(wenben.text)
            c=wenben.text
            fp.write(title + ':' + c + '\n')
            print('下载成功')

有问题群里找我,或者这里留言都可以

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/02/05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档