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正规则表达式学习指南
Aug 02 Python
Python使用plotly绘制数据图表的方法
Jul 18 Python
深入理解Python中的*重复运算符
Oct 28 Python
python 将字符串转换成字典dict的各种方式总结
Mar 23 Python
Python离线安装PIL 模块的方法
Jan 08 Python
情人节快乐! python绘制漂亮玫瑰
Aug 18 Python
python自定义线程池控制线程数量的示例
Feb 22 Python
Python OOP类中的几种函数或方法总结
Feb 22 Python
python实现月食效果实例代码
Jun 18 Python
Django实现简单网页弹出警告代码
Nov 15 Python
python实现时间序列自相关图(acf)、偏自相关图(pacf)教程
Jun 03 Python
python爬虫框架feapde的使用简介
Apr 20 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交叉表实现代码
2010/08/05 PHP
用PHP做了一个领取优惠券活动的示例代码
2019/07/05 PHP
Jquery 最近浏览过的商品的功能实现代码
2010/05/14 Javascript
Javascript技巧之不要用for in语句对数组进行遍历
2010/10/20 Javascript
jquery属性过滤选择器使用示例
2013/06/18 Javascript
js如何实现设计模式中的模板方法
2013/07/23 Javascript
浅析document.ready和window.onload的区别讲解
2013/12/18 Javascript
iframe里的页面禁止右键事件的方法
2014/06/10 Javascript
javascript属性访问表达式用法分析
2015/04/25 Javascript
AngularJs $parse、$eval和$observe、$watch详解
2016/09/21 Javascript
js数组操作方法总结(必看篇)
2016/11/22 Javascript
nodejs中方法和模块用法示例
2018/12/24 NodeJs
js实现列表向上无限滚动
2020/01/13 Javascript
vue使用nprogress加载路由进度条的方法
2020/06/04 Javascript
Vue实现图书管理案例
2021/01/20 Vue.js
Python模拟脉冲星伪信号频率实例代码
2018/01/03 Python
Python面向对象之接口、抽象类与多态详解
2018/08/27 Python
对python 读取线的shp文件实例详解
2018/12/22 Python
Python实现从SQL型数据库读写dataframe型数据的方法【基于pandas】
2019/03/18 Python
django中账号密码验证登陆功能的实现方法
2019/07/15 Python
win10安装python3.6的常见问题
2020/07/01 Python
Python性能测试工具Locust安装及使用
2020/12/01 Python
微软英国官方网站:Microsoft英国
2016/10/15 全球购物
中国电子产品外贸网站:MiniIntheBox
2017/02/06 全球购物
你常见到的runtime exception
2016/09/05 面试题
电气专业推荐信范文
2013/11/18 职场文书
优秀员工自荐书
2013/12/19 职场文书
预备党员入党思想汇报
2014/01/04 职场文书
大学生职业生涯规划书的基本内容
2014/01/06 职场文书
临床医师专业个人自我评价
2014/01/08 职场文书
学校三节实施方案
2014/06/09 职场文书
预备党员对照检查材料思想汇报
2014/09/24 职场文书
老人再婚离婚协议书范本
2014/10/27 职场文书
浅谈Redis的几个过期策略
2021/05/27 Redis
国庆节到了,利用JS实现一个生成国庆风头像的小工具 详解实现过程
2021/10/05 Javascript
vue3.0 数字翻牌组件的使用方法详解
2022/04/20 Vue.js