python爬虫之爬取文本内容(2)

一、基本案例

cpp 复制代码
#注意:需要将requests包换成2.27.1
#中文编码gbk也可能是utf-8
import requests
#from bs4 import BeautifulSoup

if __name__ == '__main__':
    url = 'https://www.biqg.cc/book/6909/1.html'#目标访问网站url
    #伪装头信息的引入
    header = {"User-Agent":
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"}
    req = requests.get(url=url,headers = header) #返回爬取网站信息
    req.encoding = 'utf-8'  #查看head中charset可以查找到编码信息
    html = req.text #转化为文本
    print(html)

二、升级案例

cpp 复制代码
# import bs4 from BeautifulSoup
# #html接上文中的已爬取得到的全部信息
#  bes= BeautifulSoup(html,"lxml")#通过lxml方式解析获取网页中文本信息
#  text = bes.find("div", id = "content"[,class_ = "<class的名称>"]) #解析text中,提取标签为"div"内id = "content"全部信息,也可解析提取class = <某名称>的内容信息


import requests
from bs4 import BeautifulSoup

if __name__ == '__main__':
    url = 'https://www.biqg.cc/book/6909/1.html'#目标访问网站url
    header = {"User-Agent":
                  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"}
    req = requests.get(url=url,headers = header)
    req.encoding = 'utf-8'
    html = req.text
    bes = BeautifulSoup(html,"lxml")
    texts = bes.find("div", class_="content")
    print(texts)

三、最终案例

cpp 复制代码
import requests
from bs4 import BeautifulSoup

if __name__ == '__main__':
    url = 'https://www.biqg.cc/book/6909/1.html'#目标访问网站url
    header = {"User-Agent":
                  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"}
    req = requests.get(url=url,headers = header)
    req.encoding = 'utf-8'
    html = req.text
    bes = BeautifulSoup(html,"lxml")
    texts = bes.find("div", class_ = "Readarea ReadAjax_content")
    # print(texts)
    texts_list = texts.text.split("\xa0" * 4) #texts.text.split("\xa0" * 4)会将texts.text按照\xa0\xa0\xa0\xa0进行分割,得到一个字符串列表,存储在texts_list中。
    texts_list = texts.text.split("\u3000" * 2)
    # print(texts_list)

    with open("D:/novel.txt","w") as file:    ##打开读写文件,逐行将列表读入文件内
        for line in texts_list:
            file.write(line+"\n")
相关推荐
该用户已不存在4 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP6 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户83562907805112 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
侃侃_天下12 小时前
最终的信号类
开发语言·c++·算法
c8i12 小时前
python中类的基本结构、特殊属性于MRO理解
python
echoarts12 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
liwulin050612 小时前
【ESP32-CAM】HELLO WORLD
python
Aomnitrix13 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
Doris_202313 小时前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_202313 小时前
Python 模式匹配match case
前端·后端·python