python數(shù)據(jù)解析BeautifulSoup爬取三國演義章節(jié)示例
數(shù)據(jù)解析
數(shù)據(jù)解析就是將爬取到的整個頁面中的局部的內容進行提取。python中常用的數(shù)據(jù)解析方式有以下三種:
- bs4(python中獨有的)
- xpath(推薦,通用型強)
- 正則
數(shù)據(jù)解析原理概述:
首先我們知道需要解析(提取)的內容都會在標簽之間或者標簽對應的屬性中進行存儲
所以我們需進行指定標簽的定位
然后將標簽或者標簽對應的屬性中存儲的數(shù)據(jù)值進行提?。ń馕觯?/p>
Beautiful Soup
Beautiful Soup 是一個可以從HTML或XML文件中提取數(shù)據(jù)的Python庫。其只能運用在python語言中
bs4數(shù)據(jù)解析原理
實例化一個BeautifulSoup對象,并且將頁面源碼數(shù)據(jù)加載到該對象中。而將頁面源碼數(shù)據(jù)加載到該對象中有兩種方式,一種是將本地得html文檔加載,另一種是將互聯(lián)網(wǎng)上獲取的頁面源碼加載通過
調用BeautifulSoup對象中相關的屬性或者方法進行標簽定位和數(shù)據(jù)提取
要使用bs4首先需要先下載對應的包
pip install bs4
pip install -i https://mirrors.aliyun.com/pypi/simple/ lxml
Beautiful Soup用法
提取整個標簽數(shù)據(jù)
進行標簽定位常用的幾個方法如下
soup.標簽名 返回的是html中第一次出現(xiàn)的標簽
soup.find(標簽名) 返回第一次出現(xiàn)的這個標簽
soup.find_all(標簽名)) 返回符合要求的所有標簽
soup.select(標簽名) 返回符合要求的所有標簽
from bs4 import BeautifulSoup #導包
html = """
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="theme-color" content="#ffffff">
<base href="./" rel="external nofollow" ><link rel="stylesheet" href="styles.30d0912c1ece284d8d9a.css" rel="external nofollow" >
</head>
<body>
<div>
<p>百里守約</p>
</div>
<div class="song">
<p>前程似錦</p>
</div>
<div class="song">
<p>前程似錦2</p>
</div>
<div class="ming"> #后面改了名字
<p>以夢為馬</p>
</div>
<div class="tang">
<ul>
<li><a title='qing'>清明時節(jié)</a></li>
<li><a title='qing'>秦時明月</a></li>
<li><a title='qing'>漢時關</a></li>
</ul>
</div>
<flink-root></flink-root>
<script type="text/javascript" src="runtime.0dcf16aad31edd73d8e8.js"></script><script type="text/javascript" src="es2015-polyfills.923637a8e6d276e6f6df.js" nomodule></script><script type="text/javascript" src="polyfills.bb2456cce5322b484b77.js"></script><script type="text/javascript" src="main.8128365baee3dc30e607.js"></script>
</body>
</html>
"""
#實例化一個BeautifulSoup對象,并且將本地的源碼數(shù)據(jù)加載到該對象中。且使用html.parser進行數(shù)據(jù)解析
soup = BeautifulSoup(html,'html.parser')
print(soup.meta) #輸出<meta charset="utf-8">
print(soup.p) #輸出<p>百里守約</p>
#find
print(soup.find('div')) #輸出<div><p>百里守約</p></div>
#這里有多個div標簽,根據(jù)屬性定位,因為class為關鍵字,所以這里加_
print(soup.find('div',class_="song")) #<p>前程似錦</p>
#find_all
print(soup.find_all('p')) #[<p>百里守約</p>, <p>前程似錦</p>, <p>前程似錦2</p>, <p>以夢為馬</p>]
print(soup.select('.tang')) #將這個選擇器中的所有內容提取
print(soup.select('.tang > ul > li > a')[1]) #返回ul中的li中的所有a標簽中的第二個a標簽 <a rel="external nofollow" title="qing">秦時明月</a>
提取標簽中的內容和標簽的屬性值
#獲取標簽中的內容
print(soup.p.text) #輸出百里守約
print(soup.find('div',class_="ming").text) #以夢為馬
print(soup.find('div',class_="song"))
print(soup.select('.tang a')[0].text) #清明時節(jié)
#獲取標簽中的屬性值,如a標簽中的href值
print(soup.select('.tang a')[0]['href']) #http://123.com
案例—爬取三國演義章節(jié)及對應的內容
網(wǎng)站如下,網(wǎng)站數(shù)據(jù)的獲取不是通過ajax發(fā)送的請求

import requests
from bs4 import BeautifulSoup
url = 'https://so.gushiwen.org/guwen/book_46653FD803893E4F7F702BCF1F7CCE17.aspx'
headers={
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Mobile Safari/537.36'
}
pag_content = requests.get(url,headers,timeout=4).text
#print(pag_content)
#提取每章節(jié)內容的鏈接
soup = BeautifulSoup(pag_content,'html.parser')
a_text = soup.select('.bookcont a') #將其下面的所有a標簽提取出來
for i in a_text:
#獲取a標簽中的href屬性的值
detail_url = i['href']
#請求詳細內容的url的內容
detail_content = requests.get(detail_url,headers,timeout=4).text
soup = BeautifulSoup(detail_content,'html.parser')
#提取class標簽
class_content = soup.find('div',class_='contson')
#print(class_content) #該標簽中有很多p標簽,返回整個class_content標簽
#print(class_content.text) #獲取其所有的內容
with open('三國演義.txt','a',encoding='utf-8') as f:
f.write(i.text+'\r')
f.write(class_content.text+'\r')
print(f'爬取{i.text}ok')
print('全部ok')


以上就是python數(shù)據(jù)解析BeautifulSoup爬取三國演義章節(jié)示例的詳細內容,更多關于BeautifulSoup爬取三國演義章節(jié)的資料請關注腳本之家其它相關文章!
- Python使用Beautiful?Soup(BS4)庫解析HTML和XML
- Python使用BeautifulSoup4修改網(wǎng)頁內容的實戰(zhàn)記錄
- python?beautifulsoup4?模塊詳情
- python?中的?BeautifulSoup?網(wǎng)頁使用方法解析
- Python爬蟲之BeautifulSoup的基本使用教程
- Python中BeautifulSoup模塊詳解
- Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解
- Python實戰(zhàn)快速上手BeautifulSoup庫爬取專欄標題和地址
- python爬蟲beautiful?soup的使用方式
相關文章
詳談Python 窗體(tkinter)表格數(shù)據(jù)(Treeview)
今天小編就為大家分享一篇詳談Python 窗體(tkinter)表格數(shù)據(jù)(Treeview),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python基本數(shù)據(jù)結構之字典類型dict用法分析
這篇文章主要介紹了Python基本數(shù)據(jù)結構之字典類型dict用法,結合實例形式分析了Python字典類型dict概念、原理、定義及基本使用技巧,需要的朋友可以參考下2019-06-06
Python文件操作JSON CSV TSV Excel和Pickle文件序列化
這篇文章主要為大家介紹了Python文件操作之JSON、CSV、TSV、Excel和Pickle文件序列化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

