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的Django框架中加载模版的方法
Jul 16 Python
详解在Python程序中自定义异常的方法
Oct 16 Python
一键搞定python连接mysql驱动有关问题(windows版本)
Apr 23 Python
Python手机号码归属地查询代码
May 04 Python
详解Python中heapq模块的用法
Jun 28 Python
numpy下的flatten()函数用法详解
May 27 Python
Python class的继承方法代码实例
Feb 14 Python
python连接mysql有哪些方法
Jun 24 Python
keras分类模型中的输入数据与标签的维度实例
Jul 03 Python
Python装饰器如何实现修复过程解析
Sep 05 Python
python speech模块的使用方法
Sep 09 Python
Python爬取用户观影数据并分析用户与电影之间的隐藏信息!
Jun 29 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 文件夹删除、php清除缓存程序
2009/08/25 PHP
Php Mssql操作简单封装支持存储过程
2009/12/11 PHP
php is_file 判断给定文件名是否为一个正常的文件
2010/05/10 PHP
php中$_REQUEST、$_POST、$_GET的区别和联系小结
2011/11/23 PHP
PHP实现查询两个数组中不同元素的方法
2016/02/23 PHP
Javascript 面向对象特性
2009/12/28 Javascript
给html超链接设置事件不使用href来完成跳
2014/04/20 Javascript
多种方法实现360浏览器下禁止自动填写用户名密码
2014/06/16 Javascript
JS使用cookie实现DIV提示框只显示一次的方法
2015/11/05 Javascript
基于jQuery和CSS3制作响应式水平时间轴附源码下载
2015/12/20 Javascript
a标签跳转到指定div,jquery添加和移除class属性的实现方法
2016/10/10 Javascript
简单理解Vue条件渲染
2016/12/03 Javascript
前端 Vue.js 和 MVVM 详细介绍
2016/12/29 Javascript
AngularJS框架的ng-app指令与自动加载实现方法分析
2017/01/04 Javascript
Angular 4环境准备与Angular cli创建项目详解
2017/05/27 Javascript
react开发教程之React 组件之间的通信方式
2017/08/12 Javascript
让Vue也可以使用Redux的方法
2018/05/23 Javascript
微信小程序实现弹出菜单功能
2018/06/12 Javascript
小程序显示弹窗时禁止下层的内容滚动实现方法
2019/03/20 Javascript
NodeJS http模块用法示例【创建web服务器/客户端】
2019/11/05 NodeJs
绘制微信小程序验证码功能的实例代码
2021/01/05 Javascript
[01:41]DOTA2超级联赛专访YYF 称一辈子难忘TI2
2013/05/28 DOTA
[00:20]TI9观赛名额抽取Ⅱ
2019/07/24 DOTA
Python调用C语言的方法【基于ctypes模块】
2018/01/22 Python
python多任务及返回值的处理方法
2019/01/22 Python
python三大神器之fabric使用教程
2019/06/10 Python
python3获取当前目录的实现方法
2019/07/29 Python
利用Python将多张图片合成视频的实现
2020/11/23 Python
Born鞋子官网:Born Shoes
2017/04/06 全球购物
Tech21美国/加拿大:英国NO.1防摔保护壳品牌
2018/01/20 全球购物
Bluebella美国官网:英国性感内衣品牌
2018/10/04 全球购物
美国在线家居装饰店:Belle&June
2018/10/24 全球购物
戴尔新西兰官网:Dell New Zealand
2020/01/07 全球购物
质量承诺书怎么写
2014/05/24 职场文书
检讨书1000字
2014/10/11 职场文书
2015年幼儿园教研活动总结
2015/03/25 职场文书