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和Ruby中each循环引用变量问题(一个隐秘BUG?)
Jun 04 Python
Python下的常用下载安装工具pip的安装方法
Nov 13 Python
Python用threading实现多线程详解
Feb 03 Python
Python使用内置json模块解析json格式数据的方法
Jul 20 Python
python实现自动获取IP并发送到邮箱
Dec 26 Python
Python中将两个或多个list合成一个list的方法小结
May 12 Python
numpy linalg模块的具体使用方法
May 26 Python
python函数修饰符@的使用方法解析
Sep 02 Python
详解python opencv、scikit-image和PIL图像处理库比较
Dec 26 Python
Kmeans均值聚类算法原理以及Python如何实现
Sep 26 Python
python3.9实现pyinstaller打包python文件成exe
Dec 13 Python
基于PyQT5制作一个桌面摸鱼工具
Feb 15 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匿名函数和use子句用法实例
2016/03/16 PHP
微信公众号模板消息群发php代码示例
2016/12/29 PHP
PHP实现的ID混淆算法类与用法示例
2018/08/10 PHP
laravel框架数据库配置及操作数据库示例
2019/10/10 PHP
javascript onkeydown,onkeyup,onkeypress,onclick,ondblclick
2009/02/04 Javascript
Jquery获取复选框被选中值的简单方法
2013/07/04 Javascript
jquery为页面增加快捷键示例
2014/01/31 Javascript
简单的JavaScript互斥锁分享
2014/02/02 Javascript
js实现滑动触屏事件监听的方法
2015/05/05 Javascript
JS实现消息来时让网页标题闪动效果的方法
2016/04/20 Javascript
jQuery原理系列-常用Dom操作详解
2016/06/07 Javascript
Bootstrap响应式导航由768px变成992px的实现代码
2017/06/15 Javascript
Node.js文件编码格式的转换的方法
2018/04/27 Javascript
开发中常用的25个JavaScript单行代码(小结)
2019/06/28 Javascript
浅入深出Vue之自动化路由
2019/08/06 Javascript
JavaScript判断数组类型的方法
2019/10/23 Javascript
[01:09:10]NB vs Liquid Supermajor小组赛 A组胜者组决赛 BO3 第一场 6.2
2018/06/04 DOTA
Python浅复制中对象生存周期实例分析
2018/04/02 Python
Python3多目标赋值及共享引用注意事项
2019/05/27 Python
django之使用celery-把耗时程序放到celery里面执行的方法
2019/07/12 Python
python3多线程知识点总结
2019/09/26 Python
Python中使用threading.Event协调线程的运行详解
2020/05/02 Python
利用Python实现Json序列化库的方法步骤
2020/09/09 Python
python爬虫中的url下载器用法详解
2020/11/30 Python
Python 随机按键模拟2小时
2020/12/30 Python
全面解析CSS Media媒体查询使用操作(推荐)
2017/08/15 HTML / CSS
使用JS+CSS3技术:让你的名字动起来
2013/04/27 HTML / CSS
波兰家具和室内装饰品购物网站:Vivre
2018/04/10 全球购物
抄作业检讨书
2014/02/17 职场文书
浪漫婚礼主持词
2014/03/14 职场文书
秋天的图画教学反思
2014/05/01 职场文书
感恩小明星事迹材料
2014/05/23 职场文书
软环境建设心得体会
2014/09/09 职场文书
中班下学期个人总结
2015/02/12 职场文书
2015年幼儿园德育工作总结
2015/05/25 职场文书
css3手动实现pc端横向滚动
2022/06/21 HTML / CSS