利用python爬取散文網(wǎng)的文章實(shí)例教程
本文主要給大家介紹的是關(guān)于python爬取散文網(wǎng)文章的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面一起來看看詳細(xì)的介紹:
效果圖如下:

配置python 2.7
bs4 requests
安裝 用pip進(jìn)行安裝 sudo pip install bs4
sudo pip install requests
簡(jiǎn)要說明一下bs4的使用因?yàn)槭桥廊【W(wǎng)頁 所以就介紹find 跟find_all
find跟find_all的不同在于返回的東西不同 find返回的是匹配到的第一個(gè)標(biāo)簽及標(biāo)簽里的內(nèi)容
find_all返回的是一個(gè)列表
比如我們寫一個(gè)test.html 用來測(cè)試find跟find_all的區(qū)別。
內(nèi)容是:
<html> <head> </head> <body> <div id="one"><a></a></div> <div id="two"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >abc</a></div> <div id="three"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >three a</a><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >three a</a><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >three a</a></div> <div id="four"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >four<p>four p</p><p>four p</p><p>four p</p> a</a></div> </body> </html>
然后test.py的代碼為:
from bs4 import BeautifulSoup
import lxml
if __name__=='__main__':
s = BeautifulSoup(open('test.html'),'lxml')
print s.prettify()
print "------------------------------"
print s.find('div')
print s.find_all('div')
print "------------------------------"
print s.find('div',id='one')
print s.find_all('div',id='one')
print "------------------------------"
print s.find('div',id="two")
print s.find_all('div',id="two")
print "------------------------------"
print s.find('div',id="three")
print s.find_all('div',id="three")
print "------------------------------"
print s.find('div',id="four")
print s.find_all('div',id="four")
print "------------------------------"運(yùn)行以后我們可以看到結(jié)果當(dāng)獲取指定標(biāo)簽時(shí)候兩者區(qū)別不大當(dāng)獲取一組標(biāo)簽的時(shí)候兩者的區(qū)別就會(huì)顯示出來

所以我們?cè)谑褂脮r(shí)候要注意到底要的是什么,否則會(huì)出現(xiàn)報(bào)錯(cuò)
接下來就是通過requests 獲取網(wǎng)頁信息了,我不太懂別人為什么要寫heard跟其他的東西
我直接進(jìn)行網(wǎng)頁訪問,通過get方式獲取散文網(wǎng)幾個(gè)分類的二級(jí)網(wǎng)頁然后通過一個(gè)組的測(cè)試,把所有的網(wǎng)頁爬取一遍
def get_html():
url = "https://www.sanwen.net/"
two_html = ['sanwen','shige','zawen','suibi','rizhi','novel']
for doc in two_html:
i=1
if doc=='sanwen':
print "running sanwen -----------------------------"
if doc=='shige':
print "running shige ------------------------------"
if doc=='zawen':
print 'running zawen -------------------------------'
if doc=='suibi':
print 'running suibi -------------------------------'
if doc=='rizhi':
print 'running ruzhi -------------------------------'
if doc=='nove':
print 'running xiaoxiaoshuo -------------------------'
while(i<10):
par = {'p':i}
res = requests.get(url+doc+'/',params=par)
if res.status_code==200:
soup(res.text)
i+=i這部分的代碼中我沒有對(duì)res.status_code不是200的進(jìn)行處理,導(dǎo)致的問題是會(huì)不顯示錯(cuò)誤,爬取的內(nèi)容會(huì)有丟失。然后分析散文網(wǎng)的網(wǎng)頁,發(fā)現(xiàn)是www.sanwen.net/rizhi/&p=1
p最大值是10這個(gè)不太懂,上次爬盤多多是100頁,算了算了以后再分析。然后就通過get方法獲取每頁的內(nèi)容。
獲取每頁內(nèi)容以后就是分析作者跟題目了代碼是這樣的
def soup(html_text):
s = BeautifulSoup(html_text,'lxml')
link = s.find('div',class_='categorylist').find_all('li')
for i in link:
if i!=s.find('li',class_='page'):
title = i.find_all('a')[1]
author = i.find_all('a')[2].text
url = title.attrs['href']
sign = re.compile(r'(//)|/')
match = sign.search(title.text)
file_name = title.text
if match:
file_name = sign.sub('a',str(title.text))獲取標(biāo)題的時(shí)候出現(xiàn)的事,請(qǐng)問大佬們寫散文你標(biāo)題加斜杠干嘛,不光加一個(gè)還有加兩個(gè)的,這個(gè)問題直接導(dǎo)致我后面寫入文件的時(shí)候文件名出現(xiàn)錯(cuò)誤,于是寫正則表達(dá)式,我給你改行了吧。
最后就是獲取散文內(nèi)容了,通過每頁的分析,獲得文章地址,然后直接獲取內(nèi)容,本來還想直接通過改網(wǎng)頁地址一個(gè)一個(gè)的獲得呢,這樣也省事了。
def get_content(url):
res = requests.get('https://www.sanwen.net'+url)
if res.status_code==200:
soup = BeautifulSoup(res.text,'lxml')
contents = soup.find('div',class_='content').find_all('p')
content = ''
for i in contents:
content+=i.text+'\n'
return content最后就是寫入文件保存ok
f = open(file_name+'.txt','w') print 'running w txt'+file_name+'.txt' f.write(title.text+'\n') f.write(author+'\n') content=get_content(url) f.write(content) f.close()
三個(gè)函數(shù)獲取散文網(wǎng)的散文,不過有問題,問題在于不知道為什么有些散文丟失了我只能獲取到大概400多篇文章,這跟散文網(wǎng)的文章是差很多很多的,但是確實(shí)是一頁一頁的獲取來的,這個(gè)問題希望大佬幫忙看看??赡軕?yīng)該做網(wǎng)頁無法訪問的處理,當(dāng)然我覺得跟我宿舍這個(gè)網(wǎng)有關(guān)系
f = open(file_name+'.txt','w') print 'running w txt'+file_name+'.txt' f.write(title.text+'\n') f.write(author+'\n') content=get_content(url) f.write(content) f.close()
差點(diǎn)忘了效果圖

能會(huì)出現(xiàn)timeout現(xiàn)象吧,只能說上大學(xué)一定要選網(wǎng)好的??!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
零基礎(chǔ)寫python爬蟲之使用urllib2組件抓取網(wǎng)頁內(nèi)容
文章詳細(xì)介紹了在python2.5環(huán)境下,如何使用urllib2這個(gè)python自帶的組件進(jìn)行抓取指定網(wǎng)頁內(nèi)容的,整個(gè)過程記錄的非常的詳細(xì),也很簡(jiǎn)單,有需要的朋友可以參考下,寫出自己的python爬蟲2014-11-11
opencv+tesseract實(shí)現(xiàn)驗(yàn)證碼識(shí)別的示例
本文主要介紹了opencv+tesseract實(shí)現(xiàn)驗(yàn)證碼識(shí)別的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
用Pelican搭建一個(gè)極簡(jiǎn)靜態(tài)博客系統(tǒng)過程解析
這篇文章主要介紹了用Pelican搭建一個(gè)極簡(jiǎn)靜態(tài)博客系統(tǒng)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
詳細(xì)介紹pandas的DataFrame的append方法使用
這篇文章主要介紹了詳細(xì)介紹pandas的DataFrame的append方法使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Pandas?時(shí)間序列分析中的resample函數(shù)
這篇文章主要介紹了Pandas?時(shí)間序列分析中的resample函數(shù),Pandas?中的resample函數(shù)用于各種頻率的轉(zhuǎn)換工作,下面我們就來看看其的參數(shù)、相關(guān)資料等,需要的小伙伴可以參考一下,希望給你帶來幫助2022-02-02
一篇文章入門Python生態(tài)系統(tǒng)(Python新手入門指導(dǎo))
原文寫于2011年末,雖然文中關(guān)于Python 3的一些說法可以說已經(jīng)不成立了,但是作為一篇面向從其他語言轉(zhuǎn)型到Python的程序員來說,本文對(duì)Python的生態(tài)系統(tǒng)還是做了較為全面的介紹2015-12-12

