利用python爬取散文網(wǎng)的文章實例教程
本文主要給大家介紹的是關(guān)于python爬取散文網(wǎng)文章的相關(guān)內(nèi)容,分享出來供大家參考學習,下面一起來看看詳細的介紹:
效果圖如下:
配置python 2.7
bs4 requests
安裝 用pip進行安裝 sudo pip install bs4
sudo pip install requests
簡要說明一下bs4的使用因為是爬取網(wǎng)頁 所以就介紹find 跟find_all
find跟find_all的不同在于返回的東西不同 find返回的是匹配到的第一個標簽及標簽里的內(nèi)容
find_all返回的是一個列表
比如我們寫一個test.html 用來測試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 "------------------------------"
運行以后我們可以看到結(jié)果當獲取指定標簽時候兩者區(qū)別不大當獲取一組標簽的時候兩者的區(qū)別就會顯示出來
所以我們在使用時候要注意到底要的是什么,否則會出現(xiàn)報錯
接下來就是通過requests 獲取網(wǎng)頁信息了,我不太懂別人為什么要寫heard跟其他的東西
我直接進行網(wǎng)頁訪問,通過get方式獲取散文網(wǎng)幾個分類的二級網(wǎng)頁然后通過一個組的測試,把所有的網(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
這部分的代碼中我沒有對res.status_code
不是200的進行處理,導致的問題是會不顯示錯誤,爬取的內(nèi)容會有丟失。然后分析散文網(wǎng)的網(wǎng)頁,發(fā)現(xiàn)是www.sanwen.net/rizhi/&p=1
p最大值是10這個不太懂,上次爬盤多多是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))
獲取標題的時候出現(xiàn)的事,請問大佬們寫散文你標題加斜杠干嘛,不光加一個還有加兩個的,這個問題直接導致我后面寫入文件的時候文件名出現(xiàn)錯誤,于是寫正則表達式,我給你改行了吧。
最后就是獲取散文內(nèi)容了,通過每頁的分析,獲得文章地址,然后直接獲取內(nèi)容,本來還想直接通過改網(wǎng)頁地址一個一個的獲得呢,這樣也省事了。
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()
三個函數(shù)獲取散文網(wǎng)的散文,不過有問題,問題在于不知道為什么有些散文丟失了我只能獲取到大概400多篇文章,這跟散文網(wǎng)的文章是差很多很多的,但是確實是一頁一頁的獲取來的,這個問題希望大佬幫忙看看。可能應該做網(wǎng)頁無法訪問的處理,當然我覺得跟我宿舍這個網(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()
差點忘了效果圖
能會出現(xiàn)timeout現(xiàn)象吧,只能說上大學一定要選網(wǎng)好的啊!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
tensorflow 實現(xiàn)打印pb模型的所有節(jié)點
今天小編就為大家分享一篇tensorflow 實現(xiàn)打印pb模型的所有節(jié)點,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python自動化測試之DDT數(shù)據(jù)驅(qū)動的實現(xiàn)代碼
這篇文章主要介紹了python自動化測試之DDT數(shù)據(jù)驅(qū)動的實現(xiàn)代碼,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07Python簡單實現(xiàn)的代理服務器端口映射功能示例
這篇文章主要介紹了Python簡單實現(xiàn)的代理服務器端口映射功能,結(jié)合實例形式分析了Python模擬服務器、代理服務器及客戶端訪問的相關(guān)操作技巧,需要的朋友可以參考下2018-04-04Matlab中關(guān)于argmax、argmin函數(shù)的使用解讀
這篇文章主要介紹了Matlab中關(guān)于argmax、argmin函數(shù)的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Python3.5 Pandas模塊之DataFrame用法實例分析
這篇文章主要介紹了Python3.5 Pandas模塊之DataFrame用法,結(jié)合實例形式詳細分析了Python3.5中Pandas模塊的DataFrame結(jié)構(gòu)創(chuàng)建、讀取、過濾、獲取等相關(guān)操作技巧與注意事項,需要的朋友可以參考下2019-04-04