国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用python爬取散文網的文章實例教程

2020-01-04 17:21:23
字體:
來源:轉載
供稿:網友

本文主要給大家介紹的是關于python爬取散文網文章的相關內容,分享出來供大家參考學習,下面一起來看看詳細的介紹:

效果圖如下:

python爬取網頁數據,python爬取網頁,python爬取動態網頁

配置python 2.7

 bs4 requests

安裝 用pip進行安裝 sudo pip install bs4

sudo pip install requests

簡要說明一下bs4的使用因為是爬取網頁 所以就介紹find 跟find_all

find跟find_all的不同在于返回的東西不同 find返回的是匹配到的第一個標簽及標簽里的內容

find_all返回的是一個列表

比如我們寫一個test.html 用來測試find跟find_all的區別。

內容是:

<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 BeautifulSoupimport lxmlif __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 "------------------------------"

運行以后我們可以看到結果當獲取指定標簽時候兩者區別不大當獲取一組標簽的時候兩者的區別就會顯示出來

python爬取網頁數據,python爬取網頁,python爬取動態網頁

所以我們在使用時候要注意到底要的是什么,否則會出現報錯

接下來就是通過requests 獲取網頁信息了,我不太懂別人為什么要寫heard跟其他的東西

我直接進行網頁訪問,通過get方式獲取散文網幾個分類的二級網頁然后通過一個組的測試,把所有的網頁爬取一遍

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的進行處理,導致的問題是會不顯示錯誤,爬取的內容會有丟失。然后分析散文網的網頁,發現是www.sanwen.net/rizhi/&p=1

p最大值是10這個不太懂,上次爬盤多多是100頁,算了算了以后再分析。然后就通過get方法獲取每頁的內容。

獲取每頁內容以后就是分析作者跟題目了代碼是這樣的

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'): 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))

獲取標題的時候出現坑爹的事,請問大佬們寫散文你標題加斜杠干嘛,不光加一個還有加兩個的,這個問題直接導致我后面寫入文件的時候文件名出現錯誤,于是寫正則表達式,我給你改行了吧。

最后就是獲取散文內容了,通過每頁的分析,獲得文章地址,然后直接獲取內容,本來還想直接通過改網頁地址一個一個的獲得呢,這樣也省事了。

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()

三個函數獲取散文網的散文,不過有問題,問題在于不知道為什么有些散文丟失了我只能獲取到大概400多篇文章,這跟散文網的文章是差很多很多的,但是確實是一頁一頁的獲取來的,這個問題希望大佬幫忙看看。可能應該做網頁無法訪問的處理,當然我覺得跟我宿舍這個破網有關系

 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()

差點忘了效果圖

python爬取網頁數據,python爬取網頁,python爬取動態網頁

能會出現timeout現象吧,只能說上大學一定要選網好的啊!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 元氏县| 青铜峡市| 辽阳市| 定边县| 厦门市| 延边| 磐石市| 郓城县| 资源县| 安远县| 湟中县| 怀化市| 安溪县| 广安市| 贞丰县| 会昌县| 芒康县| 涞源县| 扬州市| 孟州市| 营山县| 从江县| 南昌县| 崇阳县| 湘乡市| 杭锦后旗| 太仓市| 张家口市| 都兰县| 十堰市| 宁河县| 垫江县| 马鞍山市| 尚义县| 兴国县| 武隆县| 曲靖市| 岑溪市| 肇庆市| 廉江市| 得荣县|