想看,但电脑没网怎么办,python教你保存整本成TXT~

前言

嗨喽!大家好呀,这里是魔王~

模块:

  • requests >>> pip install requests
  • parsel >>> pip install parsel
  • re

环境:

  • 解释器: python 3.8
  • 编辑器: pycharm

代码实现:

  1. 发送请求
  2. 获取数据
  3. 解析数据
  4. 保存数据

代码

代码里一些东西被我删了好过审核,有需要得小伙伴可看评论或私聊我领取~

import requests     # 发送请求
import re

# 伪装
headers = {
    
    
    'cookie': '',
    'referer': '',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36',
}
url = ''
html_data = requests.get(url=url, headers=headers).text
info_list = re.findall('<h2 class="book_name"><a href="(.*?)" target="_blank" data-eid=".*?" data-cid=".*?" alt=".*?" title=".*?">(.*?)</a></h2>', html_data)
for link, title in info_list:
    link = 'https:' + link
    # print(link, title)
    # 1. 发送请求
    response = requests.get(url=link, headers=headers)
    # 2. 获取数据
    link_data = response.text
    # print(html_data)
    # 3. 解析数据
    # 网页标签 <p></p> <a></a> <div></div> <img />
    # <div class="read-content j_readContent" id=".*?">(.*?)</div>
    text = re.findall('<div class="read-content j_readContent" id=".*?">(.*?)</div>', link_data, re.S)[0]
    text = text.replace('<p>', '\n')
    text = title + '\n\n' + text
    print(text)
    # 4. 保存数据
    with open('网恋女友竟是九天神凰.txt', mode='a', encoding='utf-8') as f:
        f.write(text)

尾语

好了,我的这篇文章写到这里就结束啦!

有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

猜你喜欢

转载自blog.csdn.net/python56123/article/details/124133948