python利用beautifulSoup实现爬虫


Posted in Python onSeptember 29, 2014

以前讲过利用phantomjs做爬虫抓网页 https://3water.com/article/55789.htm 是配合选择器做的

利用 beautifulSoup(文档 :http://www.crummy.com/software/BeautifulSoup/bs4/doc/)这个python模块,可以很轻松的抓取网页内容

# coding=utf-8
import urllib
from bs4 import BeautifulSoup

url ='http://www.baidu.com/s'
values ={'wd':'网球'}
encoded_param = urllib.urlencode(values)
full_url = url +'?'+ encoded_param
response = urllib.urlopen(full_url)
soup =BeautifulSoup(response)
alinks = soup.find_all('a')

上面可以抓取百度搜出来结果是网球的记录。

beautifulSoup内置了很多非常有用的方法。

几个比较好用的特性:

构造一个node元素

soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')

tag = soup.b

type(tag)

# <class 'bs4.element.Tag'>

属性可以使用attr拿到,结果是字典

tag.attrs

# {u'class': u'boldest'}

或者直接tag.class取属性也可。

也可以自由操作属性

tag['class'] = 'verybold'
tag['id'] = 1
tag
# <blockquote class="verybold" id="1">Extremely bold</blockquote>

del tag['class']
del tag['id']
tag
# <blockquote>Extremely bold</blockquote>

tag['class']
# KeyError: 'class'
print(tag.get('class'))
# None

还可以随便操作,查找dom元素,比如下面的例子

1.构建一份文档

html_doc = """
<html><head><title>The Dormouse's story</title></head>

<p><b>The Dormouse's story</b></p>

<p>Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" id="link1">Elsie</a>,
<a href="http://example.com/lacie" id="link2">Lacie</a> and
<a href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p>...</p>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)

2.各种搞

soup.head
# <head><title>The Dormouse's story</title></head>
soup.title
# <title>The Dormouse's story</title>
soup.body.b
# <b>The Dormouse's story</b>
soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
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>]
head_tag = soup.head
head_tag
# <head><title>The Dormouse's story</title></head>

head_tag.contents
[<title>The Dormouse's story</title>]

title_tag = head_tag.contents[0]
title_tag
# <title>The Dormouse's story</title>
title_tag.contents
# [u'The Dormouse's story']
len(soup.contents)
# 1
soup.contents[0].name
# u'html'
text = title_tag.contents[0]
text.contents

for child in title_tag.children:
  print(child)
head_tag.contents
# [<title>The Dormouse's story</title>]
for child in head_tag.descendants:
  print(child)
# <title>The Dormouse's story</title>
# The Dormouse's story

len(list(soup.children))
# 1
len(list(soup.descendants))
# 25
title_tag.string
# u'The Dormouse's story'
Python 相关文章推荐
零基础写python爬虫之爬虫编写全记录
Nov 06 Python
python高阶爬虫实战分析
Jul 29 Python
在Python中获取两数相除的商和余数方法
Nov 10 Python
python爬虫获取百度首页内容教学
Dec 23 Python
python 将有序数组转换为二叉树的方法
Mar 26 Python
Python如何处理大数据?3个技巧效率提升攻略(推荐)
Apr 15 Python
PyQt5 QTable插入图片并动态更新的实例
Jun 18 Python
Python中新式类与经典类的区别详析
Jul 10 Python
Python dict和defaultdict使用实例解析
Mar 12 Python
python 已知平行四边形三个点,求第四个点的案例
Apr 12 Python
PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解
Apr 23 Python
python numpy实现rolling滚动案例
Jun 08 Python
Python中为feedparser设置超时时间避免堵塞
Sep 28 #Python
跟老齐学Python之从格式化表达式到方法
Sep 28 #Python
跟老齐学Python之print详解
Sep 28 #Python
跟老齐学Python之正规地说一句话
Sep 28 #Python
跟老齐学Python之玩转字符串(2)更新篇
Sep 28 #Python
跟老齐学Python之不要红头文件(2)
Sep 28 #Python
跟老齐学Python之不要红头文件(1)
Sep 28 #Python
You might like
php array_slice函数的使用以及参数详解
2008/08/30 PHP
PHP 开发环境配置(Zend Studio)
2010/04/28 PHP
php合并js请求的例子
2013/11/01 PHP
Android App中DrawerLayout抽屉效果的菜单编写实例
2016/03/21 PHP
PHP堆栈调试操作简单示例
2018/06/15 PHP
setTimeout函数兼容各主流浏览器运行执行效果实例
2013/06/13 Javascript
js查找节点的方法小结
2015/01/13 Javascript
PhotoShop给图片自动添加边框及EXIF信息的JS脚本
2015/02/15 Javascript
node模块机制与异步处理详解
2016/03/13 Javascript
ionic js 模型 $ionicModal 可以遮住用户主界面的内容框
2016/06/06 Javascript
jQuery.Uploadify插件实现带进度条的批量上传功能
2016/06/08 Javascript
很酷的星级评分系统原生JS实现
2016/08/25 Javascript
js编写简单的计时器功能
2017/07/15 Javascript
利用Node.js了解与测量HTTP所花费的时间详解
2017/09/22 Javascript
JavaScript实现数值自动增加动画
2017/12/28 Javascript
JS实现监控微信小程序的原理
2018/06/15 Javascript
微信小程序API—获取定位的详解
2019/04/30 Javascript
原生JS实现随机点名项目的实例代码
2019/04/30 Javascript
vue+elementUI 实现内容区域高度自适应的示例
2020/09/26 Javascript
python中sets模块的用法实例
2014/09/30 Python
JS设计模式之责任链模式实例详解
2018/02/03 Python
python定时关机小脚本
2018/06/20 Python
python里 super类的工作原理详解
2019/06/19 Python
python绘制评估优化算法性能的测试函数
2019/06/25 Python
Django用户认证系统 User对象解析
2019/08/02 Python
python画图常规设置方式
2020/03/05 Python
python微信公众号开发简单流程实现
2020/03/09 Python
Python线程协作threading.Condition实现过程解析
2020/03/12 Python
毕业生个人求职自荐信
2014/02/26 职场文书
读群众路线心得体会
2014/03/07 职场文书
白血病捐款倡议书
2014/05/14 职场文书
2014年酒店工作总结与计划
2014/11/17 职场文书
2014年度思想工作总结
2014/11/27 职场文书
高一军训决心书
2015/02/05 职场文书
PHP中strval()函数实例用法
2021/06/07 PHP
Python获取指定日期是"星期几"的6种方法
2022/03/13 Python