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 Mysql自动备份脚本
Jul 14 Python
Python多线程编程(一):threading模块综述
Apr 05 Python
Python爬虫爬取新浪微博内容示例【基于代理IP】
Aug 03 Python
python简单贪吃蛇开发
Jan 28 Python
使用PyQtGraph绘制精美的股票行情K线图的示例代码
Mar 14 Python
Python面向对象程序设计构造函数和析构函数用法分析
Apr 12 Python
python连接、操作mongodb数据库的方法实例详解
Sep 11 Python
在Python中使用filter去除列表中值为假及空字符串的例子
Nov 18 Python
关于tf.nn.dynamic_rnn返回值详解
Jan 20 Python
Python实现画图软件功能方法详解
Jul 28 Python
python 动态绘制爱心的示例
Sep 27 Python
python statsmodel的使用
Dec 21 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
2019年中国咖啡业现状与发展趋势
2021/03/04 咖啡文化
cache_lite试用
2007/02/14 PHP
php IP及IP段进行访问限制的代码
2008/12/17 PHP
php checkdate、getdate等日期时间函数操作详解
2010/03/11 PHP
几个javascript操作word的参考代码
2009/10/26 Javascript
添加JavaScript重载函数的辅助方法2
2010/07/04 Javascript
如何获取网站icon有哪些可行的方法
2014/06/05 Javascript
Javascript中数组sort和reverse用法分析
2014/12/30 Javascript
使用JavaScript脚本判断页面是否在微信中被打开
2016/03/06 Javascript
Vue.js每天必学之指令系统与自定义指令
2016/09/07 Javascript
nodejs+express实现文件上传下载管理网站
2017/03/15 NodeJs
angularjs $http实现form表单提交示例
2017/06/09 Javascript
JavaScript简介_动力节点Java学院整理
2017/06/26 Javascript
使用Node.js实现简易MVC框架的方法
2017/08/07 Javascript
React Native react-navigation 导航使用详解
2017/12/01 Javascript
使用Vue自定义指令实现Select组件
2018/05/24 Javascript
详解如何使用babel进行es6文件的编译
2018/05/29 Javascript
PostgreSQL Node.js实现函数计算方法示例
2019/02/12 Javascript
浅析JavaScript 函数柯里化
2020/09/08 Javascript
[02:04]2014DOTA2国际邀请赛 DK一个时代的落幕
2014/07/21 DOTA
[52:10]LGD vs Optic Supermajor小组赛D组胜者组决赛 BO3 第二场 6.3
2018/06/04 DOTA
[58:54]EG vs RNG 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
Python中用Descriptor实现类级属性(Property)详解
2014/09/18 Python
Python 写了个新型冠状病毒疫情传播模拟程序
2020/02/14 Python
python用opencv 图像傅里叶变换
2021/01/04 Python
浅谈HTML5中dialog元素尝鲜
2018/10/15 HTML / CSS
德国化妆品和天然化妆品网上商店:kosmetikfuchs.de
2017/06/09 全球购物
英国历史最悠久的DJ设备供应商:DJ Finance、DJ Warehouse、The DJ Shop
2019/09/04 全球购物
手术室护士自我鉴定
2013/10/14 职场文书
计算机多媒体专业自荐信
2014/07/04 职场文书
租房协议书
2014/09/12 职场文书
咖啡厅商业计划书
2014/09/15 职场文书
淘宝客服专员岗位职责
2015/04/07 职场文书
保护环境建议书作文300字
2015/09/14 职场文书
检讨书格式
2019/04/25 职场文书
Html5通过数据流方式播放视频的实现
2021/04/27 HTML / CSS