三种信息标记形式的比较

代码:

<person><firstname>Tian</firstname><lastname>Song</lastname><address><streetAddr>中关村南大街5号</streetAddr><city>北京市</city><zipcode>100081</zipcode></address><prof>Computer System</prof><prof>Security</prof>
</person>

代码:

{"firstName":"Tian","lastName":"Song","address":{"streetAddr":"中关村南大街5号","city":"北京市","zipcode":"100081"},"prof":["Computer System","Security"]
}

代码:

firstName: Tian
lastName: Song
address: streetAddr: 中关村南大街5号city: 北京市zipcode: 100081
prof:
-Computer System
-Security

信息提取的一般方法

实践:

>>> import requests
>>> r=requests.get("https://www.baidu.com/")
>>> r.encoding='UTF-8'
>>> demo=r.text
>>> from bs4 import BeautifulSoup
>>> soup=BeautifulSoup(demo,"html.parser")
>>> for link in soup.find_all('a'):print(link.get('href'))http://news.baidu.com
https://www.hao123.com
http://map.baidu.com
http://v.baidu.com
http://tieba.baidu.com
http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1
//www.baidu.com/more/
http://home.baidu.com
http://ir.baidu.com
http://www.baidu.com/duty/
http://jianyi.baidu.com/
>>> 

基于bs4库的HTML内容查找方法

练习:

>>> soup.find_all('a')
[<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>, <a class="mnav" href="https://www.hao123.com" name="tj_trhao123">hao123</a>, <a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a>, <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a>, <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a>, <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">登录</a>, <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多产品</a>, <a href="http://home.baidu.com">关于百度</a>, <a href="http://ir.baidu.com">About Baidu</a>, <a href="http://www.baidu.com/duty/">使用百度前必读</a>, <a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a>]
>>> soup.find_all(['a','b'])
[<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>, <a class="mnav" href="https://www.hao123.com" name="tj_trhao123">hao123</a>, <a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a>, <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a>, <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a>, <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">登录</a>, <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多产品</a>, <a href="http://home.baidu.com">关于百度</a>, <a href="http://ir.baidu.com">About Baidu</a>, <a href="http://www.baidu.com/duty/">使用百度前必读</a>, <a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a>]
>>> for tag in soup.find_all(True):print(tag.name)html
head
meta
meta
meta
link
title
body
div
div
div
div
div
div
img
form
input
input
input
input
input
input
span
input
span
input
div
a
a
a
a
a
noscript
a
script
a
div
div
p
a
a
p
a
a
img
>>> import re
>>> for tag in soup.find_all(re.compile('b')):print(tag.name)body
>>> 

练习:

>>> soup.find_all('a','nav')
[]
>>> soup.find_all('a','com')
[]
>>> r.text
'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write(\'<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\'+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">登录</a>\');\r\n                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'
>>> soup.find_all(id='wrapper')
[<div id="wrapper"> <div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/> </div> <form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/><span class="bg s_ipt_wr"><input autocomplete="off" autofocus="autofocus" class="s_ipt" id="kw" maxlength="255" name="wd" value=""/></span><span class="bg s_btn_wr"><input autofocus="" class="bg s_btn" id="su" type="submit" value="百度一下"/></span> </form> </div> </div> <div id="u1"> <a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a> <a class="mnav" href="https://www.hao123.com" name="tj_trhao123">hao123</a> <a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a> <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a> <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a> <noscript> <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多产品</a> </div> </div> </div> <div id="ftCon"> <div id="ftConw"> <p id="lh"> <a href="http://home.baidu.com">关于百度</a> <a href="http://ir.baidu.com">About Baidu</a> </p> <p id="cp">©2017 Baidu <a href="http://www.baidu.com/duty/">使用百度前必读</a>  <a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a> 京ICP证030173号  <img src="//www.baidu.com/img/gs.gif"/> </p> </div> </div> </div>]
>>> soup.find_all('link')
[<link href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css"/>]
>>> import re
>>> soup.find_all(id=re.compile('link'))
[]
>>> 

练习:

>>> soup.find_all('a')
[<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>, <a class="mnav" href="https://www.hao123.com" name="tj_trhao123">hao123</a>, <a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a>, <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a>, <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a>, <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">登录</a>, <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多产品</a>, <a href="http://home.baidu.com">关于百度</a>, <a href="http://ir.baidu.com">About Baidu</a>, <a href="http://www.baidu.com/duty/">使用百度前必读</a>, <a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a>]
>>> soup.find_all('a',recursive=False)
[]
>>> 

练习:

>>> soup
<!DOCTYPE html><!--STATUS OK--><html> <head><meta content="text/html;charset=utf-8" http-equiv="content-type"/><meta content="IE=Edge" http-equiv="X-UA-Compatible"/><meta content="always" name="referrer"/><link href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css"/><title>百度一下,你就知道</title></head> <body link="#0000cc"> <div id="wrapper"> <div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/> </div> <form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/><span class="bg s_ipt_wr"><input autocomplete="off" autofocus="autofocus" class="s_ipt" id="kw" maxlength="255" name="wd" value=""/></span><span class="bg s_btn_wr"><input autofocus="" class="bg s_btn" id="su" type="submit" value="百度一下"/></span> </form> </div> </div> <div id="u1"> <a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a> <a class="mnav" href="https://www.hao123.com" name="tj_trhao123">hao123</a> <a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a> <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a> <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a> <noscript> <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多产品</a> </div> </div> </div> <div id="ftCon"> <div id="ftConw"> <p id="lh"> <a href="http://home.baidu.com">关于百度</a> <a href="http://ir.baidu.com">About Baidu</a> </p> <p id="cp">©2017 Baidu <a href="http://www.baidu.com/duty/">使用百度前必读</a>  <a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a> 京ICP证030173号  <img src="//www.baidu.com/img/gs.gif"/> </p> </div> </div> </div> </body> </html>>>> soup.find_all(string="百度一下")
[]
>>> import re
>>> soup.find_all(string=re.compile("百度一下"))
['百度一下,你就知道']
>>> 

【Python爬虫】信息组织与提取方法相关推荐

  1. python爬虫如何从一个页面进入另一个页面-Python爬虫信息输入及页面的切换方法...

    实现网页的键盘输入操作 from selenium.webdriver.common.keys import Keys 动态网页有时需要将鼠标悬停在某个元素上,相应的列表选项才能显示出来. 而爬虫在工 ...

  2. python网络爬虫信息组织与提取

    提取HTML中所有URL链接 搜索到所有<a>标签 解析<a>标签格式,提取href后的链接内容! import requests r = requests.get(" ...

  3. python爬虫笔记(三):提取(二)

    信息组织和提取方法. 信息标记的三种形式 HTML的信息标记有三种信息标记方式:XML.JSON.YAML xml 2. json3. yaml 三种信息标记形式的比较 三种实例排列 比较 信息提取的 ...

  4. Python爬虫(4)网页信息

    Python爬虫(4)浅谈js JavaScript(简称"JS") 是一种具有函数优先的轻量级,解释型或即时编译型的编程语言.虽然它是作为开发Web页面的脚本语言而出名,但是它也 ...

  5. Python爬虫入门——2. 2爬取酷狗音乐top1-500歌曲信息

    有了第一个程序的基础,我们现在来爬取酷狗音乐top500的歌曲信息.连接http://www.kugou.com/yy/rank/home/1-8888.html 我们第一个程序只爬取了一个页面的数据 ...

  6. python爬虫之Beautiful Soup库,基本使用以及提取页面信息

    一.Beautiful Soup简介 爬虫正则表达式参考:Python 爬虫正则表达式和re库 在爬虫过程中,可以利用正则表达式去提取信息,但是有些人觉得比较麻烦.因为花大量时间分析正则表达式.这时候 ...

  7. 零基础入门python爬虫之《青春有你2》选手信息爬取

    零基础入门python爬虫之<青春有你2>选手信息爬取 完成<青春有你2>选手图片爬取,生成选手图片的绝对路径并输出,统计爬取的图片总数量.使用工具:requests模块.Be ...

  8. python爬虫抓取信息_python爬虫爬取网上药品信息并且存入数据库

    我最近在学习python爬虫,然后正好碰上数据库课设,我就选了一个连锁药店的,所以就把网上的药品信息爬取了下来. 1,首先分析网页 2,我想要的是评论数比较多的,毕竟好东西大概是买的人多才好.然后你会 ...

  9. Python实现信息自动配对爬虫排版程序(附下载)

    授权自AI科技大本营(ID:rgznai100) 本文约2800字,建议阅读7分钟. 本文为你介绍Python实现信息自动配对爬虫排版程序. 公众号(DatapiTHU)后台回复"20200 ...

最新文章

  1. tuple(元组)备注
  2. 转--SDP 协议分析
  3. 列出本机正在监控的端口
  4. POJ-2942:吃糖果
  5. Sage CRM升级注意事项一
  6. apache license 2.0如何使用防止法律纠纷_Thinkphp + queue 使用队列进行 邮件通知
  7. php 防止url输入,php防止伪造数据从地址栏URL提交的方法
  8. 模拟京东按s键选中输入框
  9. python语言的特点强制可读_python程序语言设计第二讲(笔记)
  10. mysql日志模式默认是raw还是_深入学习MySQL 02 日志系统:bin log,redo log,undo log
  11. Android开发之网络
  12. 云原生是一个时代下践行者们的故事
  13. 用数据分析看共享单车
  14. 服务器装系统报0x0000005d,虚拟机不能安装Win10系统,提示your PC needs to restart,错误代码0x0000005D该怎么办-电脑自学网...
  15. 笔记本计算机摄像头怎么打开,手把手教你笔记本内置摄像头打不开解决方法
  16. 测国外服务器速度的网站,美国服务器如何测试速度?
  17. 政府应用系统应用解决方案
  18. 定理在数学中的简写形式_初中数学定义、定理(大全)
  19. openwrt源码分析_Luci流程分析(openwrt下)
  20. 牺牲一个存储空间的循环队列实现方法

热门文章

  1. ip地址的分类:A、B、C、D、E五类是如何划分的?
  2. JVM—引用计数和可达性分析算法(存活性判断)
  3. Kubernetes-PersistentVolume(PV)介绍(八)
  4. 非空约束 mysql
  5. 9203学生工具页-建议放桌面
  6. 吃货开发 阶段01 类的定义 方法的布局 0925
  7. dj鲜生-用户中心-个人信息
  8. 爬虫-12-发送带参数的请求
  9. Django组件-用户认证
  10. 解决maven项目中,缺少 maven dependencies