Python3学习笔记12--urllib,BeautifulSoup

124 篇文章 0 订阅

1 介绍

urllib 是Python 的标准库,包含了从网络请求数据,处理cookie,甚至改变像请求头和用户代理这些元数据的函数。
Python3的urllib被分成3个子模块:urllib.request、urllib.parse、urllib.error。
接下来的文章中用到的是urllib.request模块中的urlopen()方法,可以用来打开并读取一个从网络获取的远程对象。

BeautifulSoup是第三方库。
BeautifulSoup能通过定位HTML 标签来格式化和组织复杂的网络信息,用简单易用的Python 对象为我们展现XML 结构信息。

Python安装第三方库,最方便的是在命令行中使用pip install …
要使用python3,在Windows命令行中输入pip3 install bs4

这里写图片描述

urllib加上BeautifulSoup,就可以搞出很多事情了

2 bsobj.tagName

bsobj.tagName 能获取页面中的第一个指定的标签.

http://www.pythonscraping.com/pages/page2.html页面有如下代码。

<html>
<head>
<title>A Useful Page</title>
</head>
<body>
<h1>An Interesting Title</h1>
<div class="body" id="fakeLatin">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</body>
</html>

Python3的IDLE中运行如下代码,可以爬取http://www.pythonscraping.com/pages/page2.html页面标签为h1的内容。

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page2.html")
bsobj = BeautifulSoup(html.read())
print(bsobj.h1)

运行结果:

<h1>An Interesting Title</h1>

3 findAll()和get_text()方法

bsobj.findAll(tagName, tagAttributes) 可以获取页面中所有指定的标签。
属性参数attributes 是用一个Python 字典封装一个标签的若干属性和对应的属性值,下面的代码中使用了namelist = bsobj.findAll(“span”,{“class”:”green”}) 语句。
.get_text() 会把超链接、段落和标签都清除掉,只剩下一串不带标签的文字。

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
bsobj = BeautifulSoup(html)

namelist = bsobj.findAll("span",{"class":"green"})
for name in namelist:
    print(name.get_text())

网页:
这里写图片描述
源码:
这里写图片描述

程序运行会输出所有属性为green的span标签里的文字。
在www.pythonscraping.com/pages/warandpeace.html对应着人名。
程序运行结果:

Anna
Pavlovna Scherer
Empress Marya
Fedorovna
Prince Vasili Kuragin
Anna Pavlovna
St. Petersburg
the prince
Anna Pavlovna
Anna Pavlovna
the prince
the prince
the prince
Prince Vasili
Anna Pavlovna
Anna Pavlovna
the prince
Wintzingerode
King of Prussia
le Vicomte de Mortemart
Montmorencys
Rohans
Abbe Morio
the Emperor
the prince
Prince Vasili
Dowager Empress Marya Fedorovna
the baron
Anna Pavlovna
the Empress
the Empress
Anna Pavlovna's
Her Majesty
Baron
Funke
The prince
Anna
Pavlovna
the Empress
The prince
Anatole
the prince
The prince
Anna
Pavlovna
Anna Pavlovna

findAll(),find()方法的定义:
findAll(tag, attributes, recursive, text, limit, keywords)
find(tag, attributes, recursive, text, keywords)

一般情况下只用传递标签名,标签属性两个参数。

参考书籍:Python网络数据采集

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值