成都免费招聘网站,常见的网站空间服务商,wordpress滑块插件,网站后台编辑框无法显示目录 一、预备知识1、Web基本工作原理2、网络爬虫的Robots协议 二、爬取网页1、请求服务器并获取网页2、查看服务器端响应的状态码3、输出网页内容 三、使用BeautifulSoup定位网页元素1、首先需要导入BeautifulSoup库2、使用find/find_all函数查找所需的标签元素 四、获取元素的… 目录 一、预备知识1、Web基本工作原理2、网络爬虫的Robots协议 二、爬取网页1、请求服务器并获取网页2、查看服务器端响应的状态码3、输出网页内容 三、使用BeautifulSoup定位网页元素1、首先需要导入BeautifulSoup库2、使用find/find_all函数查找所需的标签元素 四、获取元素的属性值五、获取元素包含的文本1、使用get_text属性查看该元素所包含的html文本2、使用text属性查看该元素及子孙元素包含的文本可能包含空白字符3、使用stripped_strings属性查看元素及其子孙包含的不带空白字符的文本 六、遍历文档元素七、练习 一、预备知识
1、Web基本工作原理
Web 服务是互联网提供的 World wide Web 服务的简称最简单的 Web 服务是如下的2层体系结构 这种浏览器和 Web 服务器交的体系结构也称为 B/S 结构文本、图片等信息在请求到达之前即通过 HTML 语言以静态网页形式存储在 Web 服务器上HTTP 请求到达后Web 服务器把网页发给客户端的浏览器进行响应属于静态网页技术。
2、网络爬虫的Robots协议
Robots 协议在网站根目录下的 robots.txt 文件用于告知网络爬虫哪些页面可以抓取哪些不行例如http://baidu.com/robots.txt Robots 协议是建议但非约束性网络爬虫可以不遵守但存在法律风险。
二、爬取网页
1、请求服务器并获取网页
假设要使用Requests库爬取网址为 http://httpbin.org/ 的网页内容主要步骤包括 1导入requests库 2调用requests.get()方法获取网页
import requests
urlhttp://httpbin.org/
response requests.get(urlurl)2、查看服务器端响应的状态码
response.status_code运行结果
200status_code等于200表示浏览器正确获取了服务器端传递过来的网页。
3、输出网页内容
print(response.text)运行结果
!DOCTYPE html
html langenheadmeta charsetUTF-8titlehttpbin.org/titlelink hrefhttps://fonts.googleapis.com/css?familyOpenSans:400,700|SourceCodePro:300,600|TitilliumWeb:400,600,700relstylesheetlink relstylesheet typetext/css href/flasgger_static/swagger-ui.csslink relicon typeimage/png href/static/favicon.ico sizes64x64 32x32 16x16 /stylehtml {box-sizing: border-box;overflow: -moz-scrollbars-vertical;overflow-y: scroll;}*,*:before,*:after {box-sizing: inherit;}body {margin: 0;background: #fafafa;}/style
/headbodya hrefhttps://github.com/requests/httpbin classgithub-corner aria-labelView source on Githubsvg width80 height80 viewBox0 0 250 250 stylefill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;aria-hiddentruepath dM0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z/pathpath dM128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2fillcurrentColor styletransform-origin: 130px 106px; classocto-arm/pathpath dM115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 ZfillcurrentColor classocto-body/path/svg/asvg xmlnshttp://www.w3.org/2000/svg xmlns:xlinkhttp://www.w3.org/1999/xlink styleposition:absolute;width:0;height:0defssymbol viewBox0 0 20 20 idunlockedpath dM15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z/path/symbolsymbol viewBox0 0 20 20 idlockedpath dM15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8zM12 8H8V5.199C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8z//symbolsymbol viewBox0 0 20 20 idclosepath dM14.348 14.849c-.469.469-1.229.469-1.697 0L10 11.819l-2.651 3.029c-.469.469-1.229.469-1.697 0-.469-.469-.469-1.229 0-1.697l2.758-3.15-2.759-3.152c-.469-.469-.469-1.228 0-1.697.469-.469 1.228-.469 1.697 0L10 8.183l2.651-3.031c.469-.469 1.228-.469 1.697 0 .469.469.469 1.229 0 1.697l-2.758 3.152 2.758 3.15c.469.469.469 1.229 0 1.698z//symbolsymbol viewBox0 0 20 20 idlarge-arrowpath dM13.25 10L6.109 2.58c-.268-.27-.268-.707 0-.979.268-.27.701-.27.969 0l7.83 7.908c.268.271.268.709 0 .979l-7.83 7.908c-.268.271-.701.27-.969 0-.268-.269-.268-.707 0-.979L13.25 10z//symbolsymbol viewBox0 0 20 20 idlarge-arrow-downpath dM17.418 6.109c.272-.268.709-.268.979 0s.271.701 0 .969l-7.908 7.83c-.27.268-.707.268-.979 0l-7.908-7.83c-.27-.268-.27-.701 0-.969.271-.268.709-.268.979 0L10 13.25l7.418-7.141z//symbolsymbol viewBox0 0 24 24 idjump-topath dM19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z //symbolsymbol viewBox0 0 24 24 idexpandpath dM10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z //symbol/defs/svgdiv idswagger-uidiv data-reactroot classswagger-uidivdiv classinformation-container wrappersection classblock col-12div classinfohgroup classmainh2 classtitlehttpbin.orgsmallpre classversion0.9.2/pre/small/h2pre classbase-url[ Base URL: httpbin.org/ ]/pre/hgroupdiv classdescriptiondiv classmarkdownpA simple HTTP Request amp; Response Service.brbrbRun locally: /bcode$ docker run -p 80:80 kennethreitz/httpbin/code/p/div/divdivdiva hrefhttps://kennethreitz.org target_blankthe developer - Website/a/diva hrefmailto:mekennethreitz.orgSend email to the developer/a/div/div!-- ADDS THE LOADER SPINNER --div classloading-containerdiv classloading/div/div/section/div/div/div/divdiv classswagger-uidiv classwrappersection classclearspan stylefloat: right;[Powered bya target_blank hrefhttps://github.com/rochacbruno/flasggerFlasgger/a]br/span/section/div/divscript src/flasgger_static/swagger-ui-bundle.js /scriptscript src/flasgger_static/swagger-ui-standalone-preset.js /scriptscript src/flasgger_static/lib/jquery.min.js typetext/javascript/scriptscriptwindow.onload function () {fetch(/spec.json).then(function (response) {response.json().then(function (json) {var current_protocol window.location.protocol.slice(0, -1);if (json.schemes[0] ! current_protocol) {// Switches scheme to the current in usevar other_protocol json.schemes[0];json.schemes[0] current_protocol;json.schemes[1] other_protocol;}json.host window.location.host; // sets the current hostconst ui SwaggerUIBundle({spec: json,validatorUrl: null,dom_id: #swagger-ui,deepLinking: true,jsonEditor: true,docExpansion: none,apisSorter: alpha,//operationsSorter: alpha,presets: [SwaggerUIBundle.presets.apis,// yay ES6 modules ↘Array.isArray(SwaggerUIStandalonePreset) ? SwaggerUIStandalonePreset : SwaggerUIStandalonePreset.default],plugins: [SwaggerUIBundle.plugins.DownloadUrl],// layout: StandaloneLayout // uncomment to enable the green top header})window.ui ui// uncomment to rename the top brand if layout is enabled// $(.topbar-wrapper .link span).replaceWith(spanhttpbin/span);})})
}/script div classswagger-uidiv classwrappersection classblock col-12 block-desktop col-12-desktopdivh2Other Utilities/h2ullia href/forms/postHTML form/a that posts to /post /forms/post/li/ulbr /br //div/section/div
/div
/body/html三、使用BeautifulSoup定位网页元素
下面给出部分网页内容用于演示如何使用BeautifulSoup查找网页上需要的内容。
htmlhtmlheadtitleThe Dormouses story/title/headbodyp classtitlebThe Dormouses story/b/pp classstoryOnce upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1Elsie/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/aanda classsister hrefhttp://example.com/tillie idlink2Tillie/a; and they lived at the bottom of a well./pp classstory爱丽丝梦游仙境/p/body/html1、首先需要导入BeautifulSoup库
参数说明html就是上面的html文档字符串html.parser指明了解析该文档字符串的解析器是html解析器。
from bs4 import BeautifulSoup
soupBeautifulSoup(html,html.parser)基本元素说明Tag标签最基本的信息组织单元分别用和/标明开头和结尾Name标签的名字p.../p的名字是’p’格式tag.nameAttributes标签的属性字典形式组织格式tag.attrsNavigableString标签内非属性字符串.../中字符串格式tag.string
2、使用find/find_all函数查找所需的标签元素
1认识html的标签元素 上面一整行是img标签它由开始标签和结束标签两部分构成标签名是img它含有src和size两个属性。
2find函数用于寻找满足条件的第一个标签
查看find函数的帮助信息
soup.find?运行结果
Signature: soup.find(nameNone, attrs{}, recursiveTrue, textNone, **kwargs)
Docstring:
Return only the first child of this Tag matching the given
criteria.
File: d:\dell\appdata\anaconda3\lib\site-packages\bs4\element.py
Type: method查找文档中的第一个p元素/标签
first_psoup.find(p)
first_p运行结果
p classtitle
bThe Dormouses story/b
/p3查看找到的元素类型和属性
#输出找到的元素类型是bs4.element.Tag类型
print(type(first_p))
#输出找到的元素的属性是一个字典
first_p.attrs运行结果
class bs4.element.Tag
{class: [title]}4find_all函数用于寻找满足条件的所有标签这些标签将被放入一个列表中
find_all函数的原型如下
find_all(self, nameNone attrsf, recursiveTrue, textNone, limitNone, **kwargs)self表明它是一个类成员函数 name是要查找的tag元素名称默认是None如果不提供就是查找所有的元素 attrs是元素的属性它是一个字典默认是空如果提供就是查找有这个指定属性的元素 recursive指定查找是否在元素节点的子树下面全范围进行默认是True 后面的text、limit、kwargs参数比较复杂将在后面用到时介绍 find_all函数返回查找到的所有指定的元素的列表每个元素是一个 bs4.element.Tag对象。
查找文档中的所有a元素
a_lssoup.find_all(a)
for a in a_ls:print(a)运行结果
a classsister hrefhttp://example.com/elsie idlink1Elsie/a
a classsister hrefhttp://example.com/lacie idlink2Lacie/a
a classsister hrefhttp://example.com/tillie idlink2Tillie/a5查找文档中classstory’的p元素
p_storysoup.find_all(p,attrs{class:story})
p_story运行结果
[p classstoryOnce upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1Elsie/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/aanda classsister hrefhttp://example.com/tillie idlink2Tillie/a; and they lived at the bottom of a well./p, p classstory爱丽丝梦游仙境/p]6练习请找出文档中classsister’的元素
all_sistersoup.find_all(attrs{class:sister})
all_sister运行结果
[a classsister hrefhttp://example.com/elsie idlink1Elsie/a, a classsister hrefhttp://example.com/lacie idlink2Lacie/a, a classsister hrefhttp://example.com/tillie idlink2Tillie/a]四、获取元素的属性值
1判断元素是否含有某属性
#判断文档中的第一个p元素是否含有class属性
first_p.has_attr(class)运行结果
True2得到元素的属性值
因为属性名和值构成字典所以采用字典的访问形式得到属性值。
#输出文档中所有a元素的href属性值
a_lssoup.find_all(a)
for a in a_ls:print(a[href])运行结果
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie五、获取元素包含的文本
先找到classstory的第一个p元素。
p_story_fstsoup.find(p,attrs{class:story})1、使用get_text属性查看该元素所包含的html文本
print(p_story_fst.get_text)运行结果
bound method Tag.get_text of p classstoryOnce upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1Elsie/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/aanda classsister hrefhttp://example.com/tillie idlink2Tillie/a; and they lived at the bottom of a well./p2、使用text属性查看该元素及子孙元素包含的文本可能包含空白字符
p_story_fst.text运行结果
\n Once upon a time there were three little sisters; and their names were\n \n Elsie\n \n ,\n \n Lacie\n \n and\n \n Tillie\n \n ; and they lived at the bottom of a well.\n 3、使用stripped_strings属性查看元素及其子孙包含的不带空白字符的文本
list(p_story_fst.stripped_strings)运行结果
[Once upon a time there were three little sisters; and their names were,Elsie,,,Lacie,and,Tillie,; and they lived at the bottom of a well.]六、遍历文档元素 1先找到classstory’的第一个p元素
p_story_fstsoup.find(p,attrs{class:story})
p_story_fst运行结果
p classstoryOnce upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1Elsie/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/aanda classsister hrefhttp://example.com/tillie idlink2Tillie/a; and they lived at the bottom of a well./p2向下遍历找到孩子元素
for child in p_story_fst.children:print(child)运行结果
Once upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1Elsie/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/aanda classsister hrefhttp://example.com/tillie idlink2Tillie/a; and they lived at the bottom of a well.
3向上遍历找到父亲元素
parntp_story_fst.parent
parnt.name运行结果
body4平行遍历找到前面的兄弟节点
list(p_story_fst.previous_siblings)运行结果
[\n, p classtitlebThe Dormouses story/b/p, \n]5平行遍历找到后面的兄弟节点
list(p_story_fst.next_siblings)运行结果
[\n, p classstory爱丽丝梦游仙境/p, \n]七、练习
testhtmlhead/headbodyspan1234
a hrefwww.test.edu.cnThis is a test!babc/b/a/span
/body/html 1写出导入BeautifulSoup库和创建BeautifulSoup对象的代码
from bs4 import BeautifulSoup
soupBeautifulSoup(test,html.parser)2完善代码使得pos能定位到指向上述html代码中的span元素节点
possoup.find(span)
pos运行结果
span1234
a hrefwww.test.edu.cnThis is a test!babc/b/a/span3完善代码能输出span元素内部包含的所有文本包含子孙元素的文本
print(pos.get_text())运行结果
1234
This is a test!abc4完善代码能输出span元素后面直接包含的文本不包含子孙元素的文本
print(pos.next_sibling.string.strip())运行结果 5找出a元素的孩子和父亲节点名称
# 定位到a元素节点
a_tagsoup.find(a)# 输出a元素的孩子节点名称
for child in a_tag.children:print(Child node name:, child.name)# 输出a元素的父亲节点名称
print(Parent node name:, a_tag.parent.name)运行结果
Child node name: None
Child node name: b
Parent node name: span6找出a元素包含的超链接信息
# 定位到a元素节点
a_tagsoup.find(a)# 获取超链接的URL
link_urla_tag.get(href)
print(Link URL:, link_url)# 获取超链接文本
link_texta_tag.get_text()
print(Link Text:, link_text)运行结果
Link URL: www.test.edu.cn
Link Text: This is a test!abc7找出a元素包含的兄弟信息
# 定位到a元素节点
a_tagsoup.find(a)# 获取下一个兄弟节点的文本内容
next_sibling_texta_tag.next_sibling.string.strip()
if a_tag.next_sibling else None
print(Next Sibling Text:, next_sibling_text)# 获取上一个兄弟节点的文本内容
prev_sibling_texta_tag.previous_sibling.string.strip()
if a_tag.previous_sibling else None
print(Previous Sibling Text:, prev_sibling_text)运行结果
Next Sibling Text: None
Previous Sibling Text: 1234