python利用beautifulSoup实现爬虫
作者:mdxy-dxy 发布时间:2022-05-17 21:10:34
标签:beautifulSoup,爬虫
以前讲过利用phantomjs做爬虫抓网页 https://www.jb51.net/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'


猜你喜欢
- 将程序转换为exe文件我们先来介绍如何使用工具Pyinstaller安装Pyinstaller我们用pip安装Pyinstaller 。注意
- 删除重复记录,将TABLE_NAME中的不重复记录保存到#TABLE_NAME中select distinct&nbs
- cursor就是一个Cursor对象,这个cursor是一个实现了迭代器(def__iter__())和生成器(yield)的MySQLdb
- 最近刚学习数据库,首先是了解数据库是什么,数据库、数据表的基本操作,这就面临了一个问题,mysql的安装,我这里下载的是64位的,基于Win
- 注意,要看懂这里,必须具备简单的Python数据分析知识,必须知道matplotlib的简单使用!例1:plt.subplot(221) #
- 如果想对一个列表做实时的更新,传统的做法是采用轮询的方式。以web为例,通过Ajax定时请求服务端然后获取数据显示在页面。这种方式实现简单,
- 前言: 当我们需要存储小数,并且有精度要求,比如存储金额时,通常会考虑使用DECIMAL字段类型,可能大部分同学只是对DECIMAL类型略有
- 本文实例讲述了python实现通过pil模块对图片格式进行转换的方法。分享给大家供大家参考。具体分析如下:python的pil模块相当的智能
- 本文实例讲述了JS实现简洁、全兼容的拖动层。分享给大家供大家参考。具体分析如下:这是一款最简洁的JS层拖动代码,全兼容ie、ff、opera
- 本文实例为大家分享了python实现人机五子棋的具体代码,供大家参考,具体内容如下图形界面引用PyQt5,还有socket通信。可以局域网对
- 下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释 <? /****************************
- 业务背景最近接到一个需求,在微信公众号界面设计一个独立界面,界面上有 A 电机进、A 电机退、B 电机进、B 电机退 4 个按钮,点击对应按
- 今天下午主要做了个实验,是针对 测试表的列,进行添加,修改,删除的。做法如下: 增加一列: alter table emp4 add tes
- ADF是oracle提供的一套企业开发的解决方案。 最近做性能测试, 需要获取当前页面有多少SQL 查询,花费多少时间。首先想到的就是常用的
- 在python中有不少对于集合迭代的方法,我们把程序运行后的再一次循环叫做迭代,每一次都循环都可以看做是一次迭代。那么在迭代结束后,我们需要
- 概述Go 语言中的 new 和 make 一直是新手比较容易混淆的东西,咋一看很相似。不过解释两者之间的不同也非常容易。new 的主要特性首
- 需求最近公司有个大屏展示项目(如下图)页面的元素需要做响应式监听,图表需要跟着窗口响应变化问题每一个图表都被我写成了一个组件,然后就在每一个
- 1、os.name---判断现在正在实用的平台,Windows返回'nt';linux返回'posix'2、
- sympy版本:1.2假设求解矩阵方程AX=A+2X其中求解之前对矩阵方程化简为(A−2E)X=A令B=(A−2E)使用qtconsole输
- 本人初学python是菜鸟级,写的不好勿喷。python爬虫用了比较简单的urllib.parse和requests,把爬来的数据显示在地图