python爬取某卢小说

目录

概要:

单章小说

1.发送请求,获取数据

2.解析数据

 保存数据

整本小说

完整代码

 

概要:

程序语言:python

第三方库:requests,BeautifulSoup

爬取网站:抗战:满级悟性,开局手搓AK_老战归来小说_全本小说下载_飞卢小说网

单章小说

1.发送请求,获取数据

# 数据请求模块
import requests

# 模拟浏览器
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
# 请求链接
url = 'https://b.faloo.com/1328711_1.html'
# 发送请求
requests = requests.get(url=url, headers=headers)
print(requests.text)

 

结果如下图所示

a07c63bc214b431abca2b9f5b3d3d507.png

2.解析数据

打开开发者工具( F12 / fn+f12 / ctrl + shift + i),点击小箭头,选中我们需要获取的元素,

c6f0c90cbdb241caa1c4453cb0b585e8.png

 找到我们需要的数据

40fa7ccd2e9e460bb8f79895a551c14a.png

通过BeautifulSoup解析数据

# 数据解析模块
from bs4 import BeautifulSoup

# 使用BeautifulSoup解析数据
soup = BeautifulSoup(requests.text, 'html.parser')

# 提取标题
full_title = soup.select_one('.c_l_title h1').text.strip()
title = full_title.split('第', 1)[1]  # 使用 split() 方法分割字符串,并选择第二个元素作为标题

# 提取小说内容
content = soup.select('.noveContent p')
chapter_content = '\n'.join([p.text.strip() for p in content])

 保存数据

# 数据持久化
with open('第'+title + '.txt', mode='a', encoding='UTF-8') as f:
    f.write(f"章节名:第{title}\n")  # 前面添加 "第" 字
    f.write(f"内容:{chapter_content}\n")

整本小说

既然知道怎么爬取单章小说了,那么只需要获取小说所有的url地址,就可以爬取全部小说内容了

68c3889f388e46dcac9b4679a83cbdff.png

我们可以看到所有的url地址都在DivTd 标签当中,但是这个url地址省略了‘https:’,是不完整的url,所以爬取下来的时候,要拼接url地址。

# 数据请求模块
import requests
from bs4 import BeautifulSoup

def get_chapter_urls(url):
    # 发送请求
    response = requests.get(url)
    # 创建BeautifulSoup对象
    soup = BeautifulSoup(response.text, 'html.parser')
    # 获取章节url
    chapters = soup.select('.DivTd a')
    for chapter in chapters:
        # 拼接章节URL
        chapter_url = 'https:' + chapter['href']
        print(chapter_url)
        
        
url = 'https://b.faloo.com/1328711.html'
get_chapter_urls(url)

11d031ebff8b436cbd481ba392a772ef.png

完整代码

import requests
from bs4 import BeautifulSoup

def get_chapter_urls(url):
    # 发送请求
    response = requests.get(url)
    # 创建BeautifulSoup对象
    soup = BeautifulSoup(response.text, 'html.parser')
    # 获取小说名字
    novel_name = soup.select_one('#novelName').text.strip()
    print(f"小说名称:{novel_name}\n")

    # 获取章节名字和url
    chapters = soup.select('.DivTd a')
    chapter_list = []
    for chapter in chapters:
        # 拼接章节URL
        chapter_url = 'https:' + chapter['href']
        # 获取完整的章节标题
        chapter_title = chapter['title']
        # 提取 "第" 字后面的部分作为新的章节标题
        chapter_title = chapter_title.split('第', 1)[1]
        # 将小说名称、章节标题和章节URL加入到章节列表中
        chapter_list.append((novel_name, chapter_title, chapter_url))
    return chapter_list

def save_chapter_content(novel_name, chapter_title, chapter_content):
    with open(f"{novel_name}.txt", mode='a', encoding='UTF-8') as f:
        f.write(f"章节:第{chapter_title}\n")
        f.write(f"内容:\n{chapter_content}\n\n")


def get_novel(url):
    # 调用get_chapter_urls函数以获取章节URL列表
    chapter_list = get_chapter_urls(url)

    for novel_name, chapter_title, chapter_url in chapter_list:
        # 发送请求以获取章节内容
        chapter_response = requests.get(chapter_url)
        # 使用BeautifulSoup解析响应文本
        chapter_soup = BeautifulSoup(chapter_response.text, 'html.parser')
        # 从解析结果中提取章节内容
        chapter_content = chapter_soup.select_one('.noveContent').text.strip()

        # 调用save_chapter_content函数保存章节内容
        save_chapter_content(novel_name, chapter_title, chapter_content)

        print(f"已保存章节:{chapter_title}")

    print("全部章节保存完成!")


url = 'https://b.faloo.com/1328711.html'
get_novel(url)

 

 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
爬取某公司招标信息的方式有很多种,以下是一种基于 Python爬取方式: 1. 分析目标网站的页面结构和数据接口,找到需要爬取的信息所在的 URL 和参数; 2. 使用 Python 的 requests 模块向目标 URL 发送网络请求,并添加合适的请求头信息; 3. 解析返回的 HTML 或 JSON 数据,提取出所需的信息; 4. 将提取的信息存储到本地文件或数据库中。 以下是一个简单的示例代码,以爬取某公司最新的招标公告为例: ```python import requests from bs4 import BeautifulSoup # 目标网站的 URL 和参数 url = 'https://www.example.com/bid/list' params = {'page': 1, 'size': 10, 'sort': 'createTime,desc'} # 发送网络请求并获取响应 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, params=params, headers=headers) # 解析 HTML 并提取信息 soup = BeautifulSoup(response.text, 'html.parser') items = soup.select('.bid-item') for item in items: title = item.select_one('.bid-item-title').text.strip() pub_time = item.select_one('.bid-item-time').text.strip() print(title, pub_time) # 存储信息到本地文件或数据库中 # ... ``` 该示例代码中,使用 requests 模块发送 GET 请求,并添加了 User-Agent 请求头信息,以模拟浏览器访问。使用 BeautifulSoup 库解析返回的 HTML 数据,并通过 CSS 选择器提取出招标公告的标题和发布时间。最后,可将提取的信息存储到本地文件或数据库中,以备后续分析和使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值