Python利用Beautifulsoup爬取笑话网站

简介:

利用Beautifulsoup爬取知名笑话网站

首先我们来看看需要爬取的网站:http://xiaohua.zol.com.cn/

1.开始前准备

1.1 python3,本篇博客内容采用python3来写,如果电脑上没有安装python3请先安装python3.

1.2 Request库,urllib的升级版本打包了全部功能并简化了使用方法。下载方法:

 
  1. pip install requests 

1.3 Beautifulsoup库, 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.。下载方法:

 
  1. pip install beautifulsoup4 

1.4 LXML,用于辅助Beautifulsoup库解析网页。(如果你不用anaconda,你会发现这个包在Windows下pip安装报错)下载方法:

 
  1. pip install lxml 

1.5 pycharm,一款功能强大的pythonIDE工具。下载官方版本后,使用license sever免费使用(同系列产品类似),具体参照http://www.cnblogs.com/hanggegege/p/6763329.html。

2.爬取过程演示与分析

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

导入需要的库,os库用来后期储存爬取内容。

随后我们点开“最新笑话”,发现有“全部笑话”这一栏,能够让我们最大效率地爬取所有历史笑话!

我们来通过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是请求头,大部分网站没有这个请求头会爬取失败

部分效果如下:

通过源码分析发现我们还是不能通过此网站就直接获取到所有笑话的信息,因此我们在在这个页面找一些间接的方法。

点开一个笑话查看全文,我们发现此时网址变成了http://xiaohua.zol.com.cn/detail58/57681.html,在点开其他的笑话,我们发现网址部都是形如http://xiaohua.zol.com.cn/detail?/?.html的格式,我们以这个为突破口,去爬取所有的内容

我们的目的是找到所有形如http://xiaohua.zol.com.cn/detail?/?.html的网址,再去爬取其内容。

我们在“全部笑话”页面随便翻到一页:http://xiaohua.zol.com.cn/new/5.html ,按下F12查看其源代码,按照其布局发现 :

每个笑话对应其中一个<li>标签,分析得每个笑话展开全文的网址藏在href当中,我们只需要获取href就能得到笑话的网址

 
  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)  

我们通过以上代码,成功获得第一页所有笑话的网址后缀:

也就是说,我们只需要获得所有的循环遍历所有的页码,就能获得所有的笑话。

上面的代码优化后:

 
  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内容后,接下来获取一个页面全部笑话的内容:

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

效果图如下:

现在我们开始存储笑话内容!开始要用到os库了

使用如下代码,获取一页笑话的所有内容!

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

效果图:

(因为我的系统为linux系统,路径问题请按照自己电脑自己更改)

我们的目标不是抓取一个页面的笑话那么简单,下一步我们要做的是把需要的页面遍历一遍!

通过观察可以得到全部笑话页面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  

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

效果图:


本文作者:宝茜滴老公

来源:51CTO

相关文章
|
1月前
|
数据采集 JSON 数据格式
python爬虫之app爬取-charles的使用
charles 基本原理,charles抓包,分析,重发。
53 0
|
1月前
|
数据采集 测试技术 API
python爬虫之app爬取-微信朋友圈
搭建appium环境,appium基本使用,API操作等等
77 0
|
2天前
|
数据采集 存储 JSON
Python爬虫面试:requests、BeautifulSoup与Scrapy详解
【4月更文挑战第19天】本文聚焦于Python爬虫面试中的核心库——requests、BeautifulSoup和Scrapy。讲解了它们的常见问题、易错点及应对策略。对于requests,强调了异常处理、代理设置和请求重试;BeautifulSoup部分提到选择器使用、动态内容处理和解析效率优化;而Scrapy则关注项目架构、数据存储和分布式爬虫。通过实例代码,帮助读者深化理解并提升面试表现。
12 0
|
6天前
|
XML 数据采集 自然语言处理
请解释Python中的BeautifulSoup库以及它的主要用途。
BeautifulSoup是Python的HTML/XML解析库,用于数据提取和网页抓取。它提供树形结构解析文档,支持查找、访问和修改元素。主要用途包括网页抓取、数据清洗、自动化测试、内容生成、网站开发及与其他库集成,如Requests和Scrapy。适用于各种数据处理场景。
9 1
|
22天前
|
前端开发 测试技术 数据库
【python】为什么使用python Django开发网站这么火?
【python】为什么使用python Django开发网站这么火?
|
29天前
|
数据采集 XML 程序员
揭秘YouTube视频世界:利用Python和Beautiful Soup的独特技术
本文介绍了如何使用Python和Beautiful Soup库抓取YouTube视频数据,包括标题、观看次数和点赞、踩的数量。通过亿牛云爬虫代理IP服务避免被网站屏蔽,提供代理服务器配置和请求头设置示例。代码可能需根据YouTube页面更新进行调整。
揭秘YouTube视频世界:利用Python和Beautiful Soup的独特技术
|
1月前
|
数据采集 存储 数据处理
使用Python爬取豆瓣电影影评:从数据收集到情感分析
本文演示如何使用Python爬虫获取豆瓣电影《肖申克的救赎》的影评数据并进行情感分析。首先,安装requests、BeautifulSoup、pandas和TextBlob库。接着,编写爬虫抓取评论的用户名、评分和内容,存储为DataFrame。然后,利用TextBlob进行情感分析,得到情感分数。此方法有助于分析用户对电影的反馈。
83 1
|
1月前
|
XML 数据采集 自然语言处理
请解释Python中的BeautifulSoup库以及它的主要用途。
请解释Python中的BeautifulSoup库以及它的主要用途。
21 0
|
1月前
|
数据采集 存储 安全
python爬虫之app爬取-mitmproxy 的使用
mitmproxy抓包原理,设置代理,MitmDump运用,mitmproxy使用。
38 0
|
1月前
|
数据采集 存储 数据挖掘
Python爬虫实战:打造一个简单的新闻网站数据爬取工具
本文将介绍如何运用Python编写一个简单而高效的网络爬虫,帮助您在实际项目中快速获取并存储新闻网站的数据。通过学习本文,您将了解到如何利用Python中的第三方库和技术来实现数据爬取,为您的数据分析和应用提供更多可能性。

热门文章

最新文章