首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 爬虫利器优美的Beauti

python 爬虫利器优美的Beauti

作者头像
py3study
发布2020-01-08 14:34:57
3860
发布2020-01-08 14:34:57
举报
文章被收录于专栏:python3python3

    近期在研究py的网络编程,编写爬虫也是顺利成章的,开始在纠结与用正则表达式来匹配,到后来发现了Beautifulsoup,用他可以非常完美的帮我完成了这些任务:

    Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。它可以大大节省你的编程时间。

   简单使用说明:

>>> from bs4 import BeautifulSoup
>>> html_doc = """
... <html><head><title>The Dormouse's story</title></head>
...  
... <p class="title"><b>The Dormouse's story</b></p>
...  
... <p class="story">Once upon a time there were three little sisters; and their names were
... <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
... <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
... <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
... and they lived at the bottom of a well.</p>
...  
... <p class="story">...</p>
... """
>>> soup = BeautifulSoup(html_doc)
>>> soup.head()
[<title>The Dormouse's story</title>]
>>> soup.title
<title>The Dormouse's story</title>
>>> soup.title.string
u"The Dormouse's story"
>>> soup.body.b
<b>The Dormouse's story</b>
>>> soup.body.a
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
>>> soup.get_text()
u"... The Dormouse's story\n...  \n... The Dormouse's story\n...  \n... Once upon a time there were three little sisters; and their names were\n... Elsie,\n... Lacie and\n... Tillie;\n... and they lived at the bottom of a well.\n...  \n... ...\n... "
>>> soup.find_all('a')
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
>>> for key in soup.find_all('a'):
...     print key.get('class'),key.get('href')
... 
['sister'] http://example.com/elsie
['sister'] http://example.com/lacie
['sister'] http://example.com/tillie

###通过里面的方法,可以很快调出里面的元素和结果:

简单说明:

soup.body:表示显示body标签下面的内容,也可以用.来叠加标签:

soup.title.string:表示现在titile的文本内容

 soup.get_text():表示显示所有文本内容:

soup.find_all():方式可以随意组合,也可以通过任意标签,包括class,id 等方式:

举例说明:以我常常看的直播表新闻为例;

1、首先看看我们要获得的内容:

wKiom1YXFvDTi66tAAT1iE-LNKQ789.jpg
wKiom1YXFvDTi66tAAT1iE-LNKQ789.jpg

我要获取的是上面那一栏热点新闻:如世预赛国足不敌卡塔而

2、源代码查看:

<div class="fb_bbs"><a href="http://news.zhibo8.cc/zuqiu/" style="padding: 0 5px 0 0;" target="_blank" title="足球新闻"><img src="/css/p_w_picpaths/football.png"/></a><span><a href="http://news.zhibo8.cc/zuqiu/" target="_blank"><font color="red"> 世预赛:国足0-1不敌卡塔
尔</font></a>|<a href="http://news.zhibo8.cc/zuqiu/2015-10-09/5616a910d74ac.htm" target="_blank">国足“刷卡”耻辱:11年不胜</a>|<a hf="http://news.zhibo8.cc/zuqiu/2015-10-09/5616b22cbd134.htm" target="_blank">切尔西签下阿梅利亚</a>|<a href="http://news.zhibo8.cc/zuqiu/2015-10-09/5616daa45ee48.htm" target="_blank">惊人!莱万5场14球</a>|<a href="http://tu.zhibo8.cc/zuqiu/" target="_blank">图-FIFA16中国球员</a></span></div>

###从源码看到,这个是一个div 标签包裹的一个class=“fb_bbs”的版块,当然我们要确保这个是唯一的。

3、用BeautifulSoup来分析出结果代码如下:

#coding=utf-8
import urllib,urllib2
from bs4 import BeautifulSoup
try:
    html = urllib2.urlopen("http://www.zhibo8.cc")
except urllib2.HTTPError as err:
    print str(err)
soup = BeautifulSoup(html)
for i in soup.find_all("div",attrs={"class":"fb_bbs"}):
    result = i.get_text().split("|")
    for term in result:
        print term
 
4、执行效果:
 
 [root@master network]# python url.py 
世预赛:国足0-1不敌卡塔尔
国足“刷卡”耻辱:11年不胜
切尔西签下阿梅利亚
惊人!莱万5场14球
图-FIFA16中国球员
利物浦官方宣布克洛普上任
档案:克洛普的安菲尔德之旅
欧预赛-德国爆冷0-1爱尔兰
葡萄牙1-0胜丹麦
图-穆帅难罢手

到此任务差不多完成,代码量比re模块少了很多,而且简洁唯美,用py做爬虫确实是个利器;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云直播
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档