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传递参数方式小结
Apr 17 Python
Windows下实现Python2和Python3两个版共存的方法
Jun 12 Python
Python函数式编程指南(一):函数式编程概述
Jun 24 Python
浅谈python socket函数中,send与sendall的区别与使用方法
May 09 Python
利用Django内置的认证视图实现用户密码重置功能详解
Nov 24 Python
python与sqlite3实现解密chrome cookie实例代码
Jan 20 Python
Django处理文件上传File Uploads的实例
May 28 Python
python中break、continue 、exit() 、pass终止循环的区别详解
Jul 08 Python
通过python实现弹窗广告拦截过程详解
Jul 10 Python
pandas如何处理缺失值
Jul 31 Python
详解Python中的文件操作
Jan 14 Python
python随机打印成绩排名表
Jun 23 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如何通过传引用的思想实现无限分类(代码简单)
2015/10/13 PHP
解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]
2020/04/06 PHP
解决PhpStorm64不能启动的问题
2020/06/20 PHP
解决 firefox 不支持 document.all的方法
2007/03/12 Javascript
javascript下拉列表菜单的实现方法
2015/11/18 Javascript
jquery插入兄弟节点的操作方法
2016/12/07 Javascript
javascript实现简易计算器
2017/02/01 Javascript
利用jQuery解析获取JSON数据
2017/04/08 jQuery
JavaScript字符串_动力节点Java学院整理
2017/06/27 Javascript
使用JavaScript根据图片获取条形码的方法
2017/07/04 Javascript
利用Ionic2 + angular4实现一个地区选择组件
2017/07/27 Javascript
不到200行 JavaScript 代码实现富文本编辑器的方法
2018/01/03 Javascript
Vue组件中prop属性使用说明实例代码详解
2018/05/31 Javascript
JavaScript实现的反序列化json字符串操作示例
2018/07/18 Javascript
Layui表格行工具事件与数据回填方法
2019/09/13 Javascript
vue路由切换之淡入淡出的简单实现
2019/10/31 Javascript
Vue学习之axios的使用方法实例分析
2020/01/06 Javascript
用Python编写分析Python程序性能的工具的教程
2015/04/01 Python
Python使用win32com实现的模拟浏览器功能示例
2017/07/13 Python
Python编程scoketServer实现多线程同步实例代码
2018/01/29 Python
Python 3.7新功能之dataclass装饰器详解
2018/04/21 Python
Python使用numpy模块创建数组操作示例
2018/06/20 Python
Python控制键盘鼠标pynput的详细用法
2019/01/28 Python
使用python实现离散时间傅里叶变换的方法
2019/09/02 Python
django 简单实现登录验证给你
2019/11/06 Python
浅谈Django QuerySet对象(模型.objects)的常用方法
2020/03/28 Python
Tensorflow中k.gradients()和tf.stop_gradient()用法说明
2020/06/10 Python
资深生产主管自我评价
2013/09/22 职场文书
高级护理专业毕业生推荐信
2013/12/25 职场文书
美食节策划方案
2014/05/26 职场文书
趵突泉导游词
2015/02/03 职场文书
会议主持词开场白
2015/05/28 职场文书
政审证明材料
2015/06/19 职场文书
入党自传范文2015
2015/06/26 职场文书
Springboot配置suffix指定mvc视图的后缀方法
2021/07/03 Java/Android
python数字转对应中文的方法总结
2021/08/02 Python