bs4-爬取小说

bs4

安装

pip install bs4
pip install lxml

bs4有两种运行方式一种是处理本地资源,一种是处理网络资源

本地

from bs4 import BeautifulSoup

if __name__ == '__main__':
    fr = open("wl.html",'r',encoding="utf8")
    soup=BeautifulSoup(fr,'lxml')
    print(soup)

网络

from bs4 import BeautifulSoup
import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}

if __name__ == '__main__':
    url="https://www.cnblogs.com/zx125/p/11404486.html"
    res=requests.get(url=url,headers=headers)
    soup=BeautifulSoup(res.text,'lxml')
    print(soup)

实例化对象的方法

soup.tagname

直接返回第一个tag标签的内容

#返回第一个a标签对象
soup.a

soup.find()

#效果和上面类似
soup.find(tagname)

#class_为tagname上的class内的属性
soup.find(tagname,class_="")
#有以下属性
class_ id attr

双重定位 属性定位 但是只拿一个

soup.find_all()

#用法和上面相同但是可以拿到满足条件的所有数据
soup.find(tagname,class_="")

soup.select()

#它支持css的选择器
select('某种选择器 #id .class 标签...'),返回的是一个列表

层级选择

soup.select('.zx > ul > li  > a')一个>表示一个层级
soup.select('.zx > ul a')也可以这样写,一个空格代表以下的任意层级,并找到所有的a

获取标签的文本内容

soup.select('.zx > ul a').tagname.text/string/get_text()

text/get_text()获取标签下面所有的文本内容

string只获取直系的文本

获取标签中的属性值

a["href"]

基本使用

from bs4 import BeautifulSoup
soup=BeautifulSoup(html_doc,'lxml') #具有容错功能
res=soup.prettify() #处理好缩进,结构化显示

案例爬取小说标题和内容

import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
def work():
    url="http://www.shicimingju.com/book/sanguoyanyi.html"
    res=requests.get(url=url,headers=headers).text
    #读取首页信息
    soup=BeautifulSoup(res,"lxml")
    #获取所有标题存在的a标签
    titles=soup.select(".book-mulu > ul > li > a")
    with open("./sangup.txt","w",encoding="utf8")as fw:
        for i in titles:
            #获取标题名称
            title=i.text
            #获取文章内容的url,并拼接成有效的请求链接
            url_title="http://www.shicimingju.com"+i['href']
            res2=requests.get(url=url_title,headers=headers).text
            soup2=BeautifulSoup(res2,"lxml")
            #获取每个章节的文章内容
            content=soup2.find("div",class_="chapter_content").text
            context_all=title+"
"+content+"
"
            #将标题和文章内容写入本地文件
            fw.write(context_all)
            print(title+"写入成功")

if __name__ == '__main__':
    work()

参考链接

https://www.cnblogs.com/xiaoyuanqujing/articles/11805757.html

https://www.jianshu.com/p/9254bdc467b2

原文地址:https://www.cnblogs.com/zx125/p/11405594.html