自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python利用Beautifulsoup爬取笑話網(wǎng)站

開發(fā) 后端
Beautiful Soup是一個可以從HTML或XML文件中提取數(shù)據(jù)的Python庫.它能夠通過你喜歡的轉(zhuǎn)換器實現(xiàn)慣用的文檔導航,查找,修改文檔的方式。Beautiful Soup會幫你節(jié)省數(shù)小時甚至數(shù)天的工作時間。

利用Beautifulsoup爬取知名笑話網(wǎng)站

首先我們來看看需要爬取的網(wǎng)站:http://xiaohua.zol.com.cn/

 

1.開始前準備

1.1 python3,本篇博客內(nèi)容采用python3來寫,如果電腦上沒有安裝python3請先安裝python3.

1.2 Request庫,urllib的升級版本打包了全部功能并簡化了使用方法。下載方法:

  1. pip install requests 

1.3 Beautifulsoup庫, 是一個可以從HTML或XML文件中提取數(shù)據(jù)的Python庫.它能夠通過你喜歡的轉(zhuǎn)換器實現(xiàn)慣用的文檔導航,查找,修改文檔的方式.。下載方法:

  1. pip install beautifulsoup4 

1.4 LXML,用于輔助Beautifulsoup庫解析網(wǎng)頁。(如果你不用anaconda,你會發(fā)現(xiàn)這個包在Windows下pip安裝報錯)下載方法:

  1. pip install lxml 

1.5 pycharm,一款功能強大的pythonIDE工具。下載官方版本后,使用license sever免費使用(同系列產(chǎn)品類似),具體參照http://www.cnblogs.com/hanggegege/p/6763329.html。

2.爬取過程演示與分析

  1. from bs4 import BeautifulSoup 
  2.  
  3. import os 
  4.  
  5. import requests  

導入需要的庫,os庫用來后期儲存爬取內(nèi)容。

隨后我們點開“***笑話”,發(fā)現(xiàn)有“全部笑話”這一欄,能夠讓我們***效率地爬取所有歷史笑話!

 

我們來通過requests庫來看看這個頁面的源代碼:

  1. from bs4 import BeautifulSoup 
  2.  
  3. import os 
  4.  
  5. import requests 
  6.  
  7. all_url = 'http://xiaohua.zol.com.cn/new/ 
  8.  
  9. headers = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  10.  
  11. all_html=requests.get(all_url,headers = headers) 
  12.  
  13. print(all_html.text)  

header是請求頭,大部分網(wǎng)站沒有這個請求頭會爬取失敗

部分效果如下: 

 

通過源碼分析發(fā)現(xiàn)我們還是不能通過此網(wǎng)站就直接獲取到所有笑話的信息,因此我們在在這個頁面找一些間接的方法。

 

點開一個笑話查看全文,我們發(fā)現(xiàn)此時網(wǎng)址變成了http://xiaohua.zol.com.cn/detail58/57681.html,在點開其他的笑話,我們發(fā)現(xiàn)網(wǎng)址部都是形如http://xiaohua.zol.com.cn/detail?/?.html的格式,我們以這個為突破口,去爬取所有的內(nèi)容

我們的目的是找到所有形如http://xiaohua.zol.com.cn/detail?/?.html的網(wǎng)址,再去爬取其內(nèi)容。

我們在“全部笑話”頁面隨便翻到一頁:http://xiaohua.zol.com.cn/new/5.html ,按下F12查看其源代碼,按照其布局發(fā)現(xiàn) :

 

每個笑話對應(yīng)其中一個<li>標簽,分析得每個笑話展開全文的網(wǎng)址藏在href當中,我們只需要獲取href就能得到笑話的網(wǎng)址

  1. from bs4 import BeautifulSoup 
  2. import os 
  3. import requests 
  4. all_url = 'http://xiaohua.zol.com.cn/new/  
  5.  
  6. headers = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  7. all_html=requests.get(all_url,headers = headers) 
  8. #print(all_html.text) 
  9. soup1 = BeautifulSoup(all_html.text,'lxml'
  10. list1=soup1.find_all('li',class_ = 'article-summary'
  11. for i in list1: 
  12.     #print(i) 
  13.     soup2 = BeautifulSoup(i.prettify(),'lxml'
  14.     list2=soup2.find_all('a',target = '_blank',class_='all-read'
  15.     for b in list2: 
  16.         href = b['href'
  17.         print(href)  

我們通過以上代碼,成功獲得***頁所有笑話的網(wǎng)址后綴: 

 

也就是說,我們只需要獲得所有的循環(huán)遍歷所有的頁碼,就能獲得所有的笑話。

上面的代碼優(yōu)化后:

  1. from bs4 import BeautifulSoup 
  2. import os 
  3. import requests 
  4. all_url = 'http://xiaohua.zol.com.cn/new/5.html  
  5.  
  6. def Gethref(url): 
  7.     headers = { 'User-Agent'"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  8.     html = requests.get(url,headers = headers) 
  9.     soup_first = BeautifulSoup(html.text,'lxml'
  10.     list_first = soup_first.find_all('li',class_='article-summary'
  11.     for i in list_first: 
  12.         soup_second = BeautifulSoup(i.prettify(),'lxml'
  13.         list_second = soup_second.find_all('a',target = '_blank',class_='all-read'
  14.         for b in list_second: 
  15.             href = b['href'
  16.             print(href) 
  17. Gethref(all_url)  

使用如下代碼,獲取完整的笑話地址url

  1. from bs4 import BeautifulSoup 
  2. import os 
  3. import requests 
  4. all_url = 'http://xiaohua.zol.com.cn/new/5.html  
  5.  
  6. def Gethref(url): 
  7.     list_href = [] 
  8.     headers = { 'User-Agent'"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  9.     html = requests.get(url,headers = headers) 
  10.     soup_first = BeautifulSoup(html.text,'lxml'
  11.     list_first = soup_first.find_all('li',class_='article-summary'
  12.     for i in list_first: 
  13.         soup_second = BeautifulSoup(i.prettify(),'lxml'
  14.         list_second = soup_second.find_all('a',target = '_blank',class_='all-read'
  15.         for b in list_second: 
  16.             href = b['href'
  17.             list_href.append(href) 
  18.     return list_href 
  19. def GetTrueUrl(liebiao): 
  20.     for i in liebiao: 
  21.         url = 'http://xiaohua.zol.com.cn  
  22.  
  23. '+str(i) 
  24.         print(url) 
  25. GetTrueUrl(Gethref(all_url)) 

簡單分析笑話頁面html內(nèi)容后,接下來獲取一個頁面全部笑話的內(nèi)容:

  1. from bs4 import BeautifulSoup 
  2. import os 
  3. import requests 
  4. all_url = 'http://xiaohua.zol.com.cn/new/5.html  
  5.  
  6. def Gethref(url): 
  7.     list_href = [] 
  8.     headers = { 'User-Agent'"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  9.     html = requests.get(url,headers = headers) 
  10.     soup_first = BeautifulSoup(html.text,'lxml'
  11.     list_first = soup_first.find_all('li',class_='article-summary'
  12.     for i in list_first: 
  13.         soup_second = BeautifulSoup(i.prettify(),'lxml'
  14.         list_second = soup_second.find_all('a',target = '_blank',class_='all-read'
  15.         for b in list_second: 
  16.             href = b['href'
  17.             list_href.append(href) 
  18.     return list_href 
  19. def GetTrueUrl(liebiao): 
  20.     list = [] 
  21.     for i in liebiao: 
  22.         url = 'http://xiaohua.zol.com.cn  
  23.  
  24. '+str(i) 
  25.         list.append(url) 
  26.     return list 
  27. def GetText(url): 
  28.     for i in url: 
  29.         html = requests.get(i) 
  30.         soup = BeautifulSoup(html.text,'lxml'
  31.         content = soup.find('div',class_='article-text'
  32.         print(content.text) 
  33. GetText(GetTrueUrl(Gethref(all_url)))  

效果圖如下:

 

現(xiàn)在我們開始存儲笑話內(nèi)容!開始要用到os庫了

使用如下代碼,獲取一頁笑話的所有內(nèi)容!

  1. from bs4 import BeautifulSoup 
  2. import os 
  3. import requests 
  4. all_url = 'http://xiaohua.zol.com.cn/new/5.html  
  5.  
  6. os.mkdir('/home/lei/zol'
  7. def Gethref(url): 
  8.     list_href = [] 
  9.     headers = { 'User-Agent'"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  10.     html = requests.get(url,headers = headers) 
  11.     soup_first = BeautifulSoup(html.text,'lxml'
  12.     list_first = soup_first.find_all('li',class_='article-summary'
  13.     for i in list_first: 
  14.         soup_second = BeautifulSoup(i.prettify(),'lxml'
  15.         list_second = soup_second.find_all('a',target = '_blank',class_='all-read'
  16.         for b in list_second: 
  17.             href = b['href'
  18.             list_href.append(href) 
  19.     return list_href 
  20. def GetTrueUrl(liebiao): 
  21.     list = [] 
  22.     for i in liebiao: 
  23.         url = 'http://xiaohua.zol.com.cn  
  24.  
  25. '+str(i) 
  26.         list.append(url) 
  27.     return list 
  28. def GetText(url): 
  29.     for i in url: 
  30.         html = requests.get(i) 
  31.         soup = BeautifulSoup(html.text,'lxml'
  32.         content = soup.find('div',class_='article-text'
  33.         title = soup.find('h1',class_ = 'article-title'
  34.         SaveText(title.text,content.text) 
  35. def SaveText(TextTitle,text): 
  36.     os.chdir('/home/lei/zol/'
  37.     f = open(str(TextTitle)+'txt','w'
  38.     f.write(text) 
  39.     f.close() 
  40. GetText(GetTrueUrl(Gethref(all_url)))  

效果圖:

 

(因為我的系統(tǒng)為linux系統(tǒng),路徑問題請按照自己電腦自己更改)

我們的目標不是抓取一個頁面的笑話那么簡單,下一步我們要做的是把需要的頁面遍歷一遍!

通過觀察可以得到全部笑話頁面url為http://xiaohua.zol.com.cn/new/+頁碼+html,接下來我們遍歷前100頁的所有笑話,全部下載下來!

接下來我們再次修改代碼:

  1. from bs4 import BeautifulSoup 
  2. import os 
  3. import requests 
  4. num = 1 
  5. url = 'http://xiaohua.zol.com.cn/new/  
  6.  
  7. '+str(num)+'.html' 
  8. os.mkdir('/home/lei/zol'
  9. def Gethref(url): 
  10.     list_href = [] 
  11.     headers = { 'User-Agent'"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  12.     html = requests.get(url,headers = headers) 
  13.     soup_first = BeautifulSoup(html.text,'lxml'
  14.     list_first = soup_first.find_all('li',class_='article-summary'
  15.     for i in list_first: 
  16.         soup_second = BeautifulSoup(i.prettify(),'lxml'
  17.         list_second = soup_second.find_all('a',target = '_blank',class_='all-read'
  18.         for b in list_second: 
  19.             href = b['href'
  20.             list_href.append(href) 
  21.     return list_href 
  22. def GetTrueUrl(liebiao): 
  23.     list = [] 
  24.     for i in liebiao: 
  25.         url = 'http://xiaohua.zol.com.cn  
  26.  
  27. '+str(i) 
  28.         list.append(url) 
  29.     return list 
  30. def GetText(url): 
  31.     for i in url: 
  32.         html = requests.get(i) 
  33.         soup = BeautifulSoup(html.text,'lxml'
  34.         content = soup.find('div',class_='article-text'
  35.         title = soup.find('h1',class_ = 'article-title'
  36.  
  37.         SaveText(title.text,content.text) 
  38. def SaveText(TextTitle,text): 
  39.     os.chdir('/home/lei/zol/'
  40.     f = open(str(TextTitle)+'txt','w'
  41.     f.write(text) 
  42.     f.close() 
  43. while num<=100: 
  44.     url = 'http://xiaohua.zol.com.cn/new/  
  45.  
  46. ' + str(num) + '.html' 
  47.     GetText(GetTrueUrl(Gethref(url))) 
  48.     num=num+1  

大功告成!剩下的等待文件下載完全就行拉!

效果圖: 

 

責任編輯:龐桂玉 來源: 寶茜滴老公的博客
相關(guān)推薦

2021-01-24 16:40:00

Python爬取網(wǎng)站編程語言

2021-03-18 09:18:12

python爬蟲

2016-12-07 11:18:58

Python爬蟲網(wǎng)站

2017-06-14 15:20:43

Python爬蟲BeautifulSo

2024-10-08 10:44:32

2022-08-02 11:24:22

菜鳥Python網(wǎng)站自動簽到

2014-03-11 11:21:23

2017-12-14 21:45:39

2017-06-07 10:00:56

PythonBeautifulSo解析器

2016-11-07 15:23:37

Python

2016-12-08 16:47:06

2020-07-14 10:19:29

Python 開發(fā)運維

2024-01-05 08:58:36

2010-05-24 10:58:09

SVN更新網(wǎng)站

2013-06-24 10:12:27

Jego中國移動Just Easy G

2017-12-07 20:10:44

PythonGithub數(shù)據(jù)

2025-04-22 09:39:46

Python爬蟲網(wǎng)頁數(shù)據(jù)抓取

2020-10-16 07:03:17

Scrapy爬蟲框架

2020-09-11 09:10:18

編程程序員開發(fā)

2021-12-13 09:13:48

網(wǎng)站數(shù)據(jù)客戶數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號