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之模拟鼠标键盘动作具体实现
Dec 30 Python
在Python中编写数据库模块的教程
Apr 29 Python
Python实现SSH远程登陆,并执行命令的方法(分享)
May 08 Python
Python 稀疏矩阵-sparse 存储和转换
May 27 Python
Python编程pygal绘图实例之XY线
Dec 09 Python
CentOS 7下安装Python3.6 及遇到的问题小结
Nov 08 Python
对Python强大的可变参数传递机制详解
Jun 13 Python
Python 多线程共享变量的实现示例
Apr 17 Python
Python如何在main中调用函数内的函数方式
Jun 01 Python
学习python需要有编程基础吗
Jun 02 Python
django filter过滤器实现显示某个类型指定字段不同值方式
Jul 16 Python
Python+unittest+DDT实现数据驱动测试
Nov 30 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
DC最新动画电影:《战争之子》为何内容偏激,毁了一个不错的漫画
2020/04/09 欧美动漫
php使用ICQ网关发送手机短信
2013/10/30 PHP
PHP操作mysql数据库分表的方法
2016/06/09 PHP
PHP编写文件多服务器同步程序
2016/07/02 PHP
PHP实现正则匹配所有括号中的内容
2018/06/22 PHP
laravel多条件查询方法(and,or嵌套查询)
2019/10/09 PHP
一个简单的网站访问JS计数器 刷新1次加1次访问
2012/09/20 Javascript
jQuery实现的鼠标滑过弹出放大图片特效
2016/01/08 Javascript
实例解析js中try、catch、finally的执行规则
2017/02/24 Javascript
原生JS实现轮播图效果
2018/10/12 Javascript
Vue中computed、methods与watch的区别总结
2019/04/10 Javascript
js实现继承的方法及优缺点总结
2019/05/08 Javascript
Vue分页插件的前后端配置与使用
2019/10/09 Javascript
微信小程序 自定义弹窗实现过程(附代码)
2019/12/05 Javascript
Python中使用urllib2模块编写爬虫的简单上手示例
2016/01/20 Python
浅谈Python 对象内存占用
2016/07/15 Python
python数据结构之链表详解
2017/09/12 Python
python编程实现随机生成多个椭圆实例代码
2018/01/03 Python
python+os根据文件名自动生成文本
2019/03/21 Python
python 并发编程 非阻塞IO模型原理解析
2019/08/20 Python
解决pycharm安装第三方库失败的问题
2020/05/09 Python
python 使用tkinter+you-get实现视频下载器
2020/11/17 Python
Berghaus官网:户外服装和设备,防水服
2020/01/17 全球购物
运动会入场式解说词
2014/02/18 职场文书
超市商业计划书
2014/05/04 职场文书
思想作风纪律整顿心得体会
2014/09/04 职场文书
迎国庆横幅标语
2014/10/08 职场文书
幼儿园教学工作总结2015
2015/05/12 职场文书
2016年党员创先争优承诺书
2016/03/25 职场文书
手把手教你制定暑期学习计划,让你度过充实的暑假
2019/08/22 职场文书
“爱眼护眼,提前预防近视”倡议书3篇
2019/10/30 职场文书
python 定义函数 返回值只取其中一个的实现
2021/05/21 Python
Python数据可视化之用Matplotlib绘制常用图形
2021/06/03 Python
python 爬取哔哩哔哩up主信息和投稿视频
2021/06/07 Python
python异常中else的实例用法
2021/06/15 Python
java实现对Hadoop的操作
2021/07/01 Java/Android