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实现过滤单个Android程序日志脚本分享
Jan 16 Python
python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法
May 24 Python
python的re正则表达式实例代码
Jan 24 Python
Django migrations 默认目录修改的方法教程
Sep 28 Python
解决Python3用PIL的ImageFont输出中文乱码的问题
Aug 22 Python
关于Python形参打包与解包小技巧分享
Aug 24 Python
Python爬取破解无线网络wifi密码过程解析
Sep 17 Python
Python常用模块logging——日志输出功能(示例代码)
Nov 20 Python
python实现udp传输图片功能
Mar 20 Python
python使用列表的最佳方案
Aug 12 Python
numba提升python运行速度的实例方法
Jan 25 Python
python基础学习之生成器与文件系统知识总结
May 25 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 eval函数用法 PHP中eval()函数小技巧
2012/10/31 PHP
基于php的CMS中展示文章类实例分析
2015/06/18 PHP
PHP生成随机码的思路与方法实例探索
2019/04/11 PHP
(currentStyle)javascript为何有时用style得不到已设定的CSS的属性
2007/08/15 Javascript
javascript 模拟点击广告
2010/01/02 Javascript
JS将数字转换成三位逗号分隔的样式(示例代码)
2014/02/19 Javascript
JS中使用Array函数shift和pop创建可忽略参数的例子
2014/05/28 Javascript
AngularJS入门教程之路由与多视图详解
2016/08/19 Javascript
JS识别浏览器类型(电脑浏览器和手机浏览器)
2016/11/18 Javascript
jQuery实现判断控件是否显示的方法
2017/01/11 Javascript
js中less常用的方法小结
2017/08/09 Javascript
vue axios 表单提交上传图片的实例
2018/03/16 Javascript
VSCode中如何利用d.ts文件进行js智能提示
2018/04/13 Javascript
css配合JavaScript实现tab标签切换效果
2018/10/11 Javascript
Javascript中parseInt的正确使用方式
2018/10/17 Javascript
vue-cli配置flexible过程详解
2019/07/04 Javascript
微信小程序 调用远程接口 给全局数组赋值代码实例
2019/08/13 Javascript
vue 动态组件用法示例小结
2020/03/06 Javascript
关于JavaScript中异步/等待的用法与理解
2020/11/18 Javascript
总结Python中逻辑运算符的使用
2015/05/13 Python
python绘制简单彩虹图
2018/11/19 Python
Python 的AES加密与解密实现
2019/07/09 Python
Python将文字转成语音并读出来的实例详解
2019/07/15 Python
python绘制玫瑰的实现代码
2020/03/02 Python
python3用urllib抓取贴吧邮箱和QQ实例
2020/03/10 Python
python有几个版本
2020/06/17 Python
keras实现theano和tensorflow训练的模型相互转换
2020/06/19 Python
详解基于python的全局与局部序列比对的实现(DNA)
2020/10/07 Python
HTML5 常见面试题之PC端和移动端区别介绍
2018/01/22 HTML / CSS
Yves Rocher捷克官方网站:植物化妆品的创造者
2019/07/31 全球购物
文员自我评价怎么写
2013/09/19 职场文书
共产党员批评与自我批评
2014/10/15 职场文书
2015年护士节慰问信
2015/03/23 职场文书
2015年安全生产工作总结范文
2015/04/02 职场文书
亮剑观后感300字
2015/06/05 职场文书
班干部竞选演讲稿(精选5篇)
2019/09/24 职场文书