使用Python爬取所需要的文章

平时喜欢看一些书,加上朋友有时候也喜欢让我给他爬取一些小说,趁最近空闲下来就简单的记录一下自己写爬虫的过程吧

首先需要导入相关的模块

import requests
from lxml import etree 

安装对应模块的方式

# pip快速安装
pip install requests
pip install lxml

向网站发送请求并获取网站数据

在这里插入图片描述

  • 如图所示红框的地方为这本小说的网址:网页地址
  • 于是就可以获取到网页数据:
def book():
    url = "https://www.xbiquge.la/7/7194/"
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = etree.HTML(response.text)
注意:
  • 网页编码格式需要打开开发者工具查看,如下图:

如何获取正文地址和章节名称

  • 图中红框的表示小说的正文章节内容和章节名称于是可以获取到对应的信息
    XPath 语法
  • 对于要想快速定位到对应内容的位置,可以用chrome浏览器的插件
    XPath Helper

如图XPath Helper使用效果

def book():
    url = "https://www.xbiquge.la/7/7194/"
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = etree.HTML(response.text)
    url_list = html.xpath('//div[@id="list"]/dl/dd/a/@href')
    name_list = html.xpath('//div[@id="list"]/dl/dd/a/text()')

获取正文内容

def book():
    url = "https://www.xbiquge.la/7/7194/"
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = etree.HTML(response.text)
    url_list = html.xpath('//div[@id="list"]/dl/dd/a/@href')
    name_list = html.xpath('//div[@id="list"]/dl/dd/a/text()')
    for ur, na in zip(url_list, name_list):
        res = requests.get(f'https://www.xbiquge.la{ur}')      # 向网站发送请求并获取网站数据
        res.encoding = 'utf-8'
        res_html = etree.HTML(res.text)
        info = res_html.xpath('//div[@id="content"]/text()')

然后把正文内容写入到文件中就完成了

  • 完整代码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
from lxml import etree


def book():
    url = "https://www.xbiquge.la/7/7194/"
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = etree.HTML(response.text)
    url_list = html.xpath('//div[@id="list"]/dl/dd/a/@href')
    name_list = html.xpath('//div[@id="list"]/dl/dd/a/text()')
    fp = open("修真聊天群.txt", 'w')
    for ur, na in zip(url_list, name_list):
        res = requests.get(f'https://www.xbiquge.la{ur}')      # 向网站发送请求并获取网站数据
        res.encoding = 'utf-8'
        res_html = etree.HTML(res.text)
        info = res_html.xpath('//div[@id="content"]/text()')
        fp.write(f'{na}\n\n')
        print(f'{na}__{ur}')        # 查看当前章节名称和链接地址
        for i in info:
            i = i.replace(r'\xa0', '').replace('\n\n', '\n')        # 去除垃圾信息并调整排版
            if i == '\r':
                continue
            fp.write(i)         # 写入正文到文本中
        fp.write('\n\n')
    fp.close()


if __name__ == '__main__':
    book()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦落影逝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值