python爬虫 爬取诗词名句网

7 篇文章 0 订阅
3 篇文章 0 订阅
  • 使用requests库,xpath库
import requests
import time
from lxml import etree


# 去请求页面的函数
def request_Header(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
    }

    request = requests.get(url=url,headers=headers)
    return request


def get_content_text(all_href):

    request = request_Header(all_href)
    etrees = etree.HTML(request.content)
    # 获取所有章节的内容  得到一个列表 这是由多个p标签组成的
    content_text = etrees.xpath('//div[@class="layui-col-md8 layui-col-sm7"]/div/div/p/text()')
    strs_cont = ' '
    for con in content_text:
        strs_cont+=con+'\n'
    print(strs_cont)
    return strs_cont




def main():
    url = 'http://www.shicimingju.com/book/sanguoyanyi.html'
    req = request_Header(url)
    # 得到网页的内容
    content = req.content
    etrees = etree.HTML(content)
    # 获取所有章节内容
    text = etrees.xpath('//div[@class="book-mulu"]/ul/li/a')
    fp = open('三国演义.txt','w',encoding='utf8')
    # 遍历这个列表,挨个获取
    for a in text:
        # 获取标题
        title = a.xpath('text()')[0]
        print('正在下载>>>%s'%title)
        # 获取连接
        href = a.xpath('@href')[0]
        # 拼接url
        all_href = 'http://www.shicimingju.com' + str(href)
        # print(all_href)
        # 去网页中获取内容
        get_content = get_content_text(all_href)
        fp.write(title+'\n' + str(get_content)+'\n')
        # time.sleep(2)
        print('结束下载%s' % title)
    fp.close()

if __name__ == '__main__':
    main()
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值