当前位置: 首页 > news >正文

织梦网站识别php网站数据库修改

织梦网站识别,php网站数据库修改,哪些网站可以做团购,上传网站图片处理我们通过Requests请求url获取数据#xff0c;请求把数据返回来之后就要提取目标数据#xff0c;不同的网站返回的内容通常有多种不同的格式#xff0c;一种是 json 格式#xff0c;我们可以直接通过json.loads转换python的json对象处理。另一种 XML 格式的#xff0c;还有…我们通过Requests请求url获取数据请求把数据返回来之后就要提取目标数据不同的网站返回的内容通常有多种不同的格式一种是 json 格式我们可以直接通过json.loads转换python的json对象处理。另一种 XML 格式的还有一种最常见格式的是 HTML 文档今天就来讲讲如何从 HTML 中提取出感兴趣的数据。 BeautifulSoup 是一个用于解析 HTML 文档的 Python 库通过 BeautifulSoup你只需要用很少的代码就可以提取出 HTML 中任何感兴趣的内容此外它还有一定的 HTML 容错能力对于一个格式不完整的HTML 文档它也可以正确处理。 安装 beautifulsoup pip install beautifulsoup4初始化对象时可以直接传递字符串或者文件句柄 soup BeautifulSoup(open(index.html)) soup BeautifulSoup(htmldata/html)支持多种解析接口 # python内置HTML解析 BeautifulSoup(markup, html.parser) # lxml语言支持HTML解析 BeautifulSoup(markup, lxml) # 解析XML引擎 BeautifulSoup(markup, xml) # 解析HTML5引擎 BeautifulSoup(markup, html5lib) 自动添加和补全标签 下面是一段不规范的html缺少闭合标签 html_doc htmlheadtitleThe Dormouses story/title/head body p classtitlebThe Dormouses story/b/pp classstoryOnce upon a time there were three little sisters; and their names were a hrefhttp://example.com/elsie classsister idlink1Elsie/a, a hrefhttp://example.com/lacie classsister idlink2Lacie/a and a hrefhttp://example.com/tillie classsister idlink3Tillie/a; and they lived at the bottom of a well./pp classstory.../p它由很多标签Tag组成比如 html、head、title等等都是标签一个标签对构成一个节点比如 …是一个根节点节点之间存在某种关系比如p之间互为邻居他们是相邻的兄弟sibling节点p 是 body 的直接子children节点还是 html 的子孙descendants节点body 是 p 的父parent节点html 是 p 的祖辈parents节点嵌套在标签之间的字符串是该节点下的一个特殊子节点比如title文本内容“The Dormouse’sstory”也是一个节点只不过没名字。 from bs4 import BeautifulSoup soup BeautifulSoup(html_doc, html.parser) print(soup.prettify())prettify()标准缩进格式的输出。输出内容如下 htmlheadtitleThe 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/htmlbs4获取标签及内容示例 # title标签 soup.title # titleThe Dormouses story/title# title标签名称 soup.title.name # title# # title标签的文本字符内容 soup.title.string # The Dormouses story# title标签父节点名称 soup.title.parent.name # head# 从前向后找到html孙节点第一个p节点 soup.p # p classtitlebThe Dormouses story/b/p# p节点的class属性 soup.p[class] # [title]# 进栈出栈的方式找到第一个a标签 soup.a # a classsister hrefhttp://example.com/elsie idlink1Elsie/a# p节点的href属性 soup.a[href] # http://example.com/elsiesoup.find_all(a) # 同上 soup.find_all(p)[1].find_all(a)# [a classsister hrefhttp://example.com/elsie idlink1Elsie/a, # a classsister hrefhttp://example.com/lacie idlink2Lacie/a, # a classsister hrefhttp://example.com/tillie idlink3Tillie/a]soup.find(idlink3) # a classsister hrefhttp://example.com/tillie idlink3Tillie/a遍历文档树 从根节点 html 标签开始遍历元素进栈出栈直到找到目标元素为止。 BeatifulSoup 将 HTML 抽象成为 4 类主要的数据类型: Tag每个标签节点就是一个Tag对象NavigableString包裹在Tag对象里的文本字符串BeautifulSoup 对象代表要解析的整个Comment注释对象 type(soup) # class bs4.BeautifulSoup type(soup.p) # class bs4.element.Tag # type(soup.p.string) class bs4.element.NavigableStringTag标签 每个 Tag 都有一个名字它对应 HTML 的标签名称。 soup.p.name # p标签有属性属性的访问方式和字典是类似的它返回一个列表对象或字符串。 soup.p[class] # [title]soup.a[href] # http://example.com/elsieNavigableString 获取标签中的内容直接使用 .stirng 即可获取它是一个 NavigableString 对象 。 soup.p.string # The Dormouses story type(soup.p.string) bs4.element.NavigableString搜索文档树 搜索文档树是通过指定标签名来搜索元素还可以通过指定标签的属性值来精确定位某个节点元素最常用的两个方法就是 find 和 find_all。这两个方法在 BeatifulSoup 和 Tag 对象上都可以被调用。 find_all()方法 find_all( name , attrs , recursive , text , **kwargs )第一个参数 name 是标签节点的名字。 # 所有p标签 soup.find_all(p)# [p classtitlebThe Dormouses story/b/p, # p classstoryOnce upon a time there were three little sisters; and their names were # a classsister hrefhttp://example.com/elsie idlink1Elsie/a, # a classsister hrefhttp://example.com/lacie idlink2Lacie/a and # a classsister hrefhttp://example.com/tillie idlink3Tillie/a; # and they lived at the bottom of a well./p, # p classstory.../p]第二个参数是标签的class属性值 soup.find_all(p,title) # 同上 soup.find_all(p,class_ title)# [p classtitlebThe Dormouses story/b/p]kwargs 是标签的属性名值对。 import re # 支持使用标签属性 soup.find_all(hrefhttp://example.com/lacie) soup.find_all(idlink2)# 支持使用正则 soup.find_all(hrefre.compile(lacie))# [a classsister hrefhttp://example.com/lacie idlink2Lacie/a]# 支持使用布尔类型 soup.find_all(a,idTrue)遍历和搜索相结合先定位到 body 标签再从 body 中找 a 标签.。 soup.body.find_all(a,idTrue)find()方法 find 方法跟 find_all 类似唯一不同的地方是它返回的单个 Tag 对象而非列表如果没找到匹配的节点则返回 None。如果匹配多个 Tag只返回第0个。 soup.body.find(a) # a classsister hrefhttp://example.com/elsie idlink1Elsie/aget_text()方法 获取标签里面内容除了可以使用 .string 之外还可以使用 get_text 方法不同的地方在于前者返回的一个 NavigableString 对象后者返回的是 字符串。 soup.body.find(a).get_text() # Elsie实际场景中我们一般使用 get_text 方法获取标签中的内容。 总结 通过beautifulsoup我们能够解析大部分静态html网页遍历和搜索组合方式定位html的标签并获取相应标签的内容。
http://www.pierceye.com/news/561470/

相关文章:

  • 廊坊网站建设设计wordpress上传后如何访问
  • 禅城网站制作网站推广优化技巧大全
  • xampp做的网站能搜索吗广州外贸公司集中地
  • 茶网站建设网站的基础建设
  • 大型机械网站建设公司免费的cms视频网站
  • 杭州做网站哪家好在线设计平台都有哪些比较好用的
  • 内外网网站栏目建设方案专门做电容的网站
  • 一般网站字体多大有没有做公司网站的
  • 做国外进口衣服的网站好怎么推广公众号
  • 安县建设局网站网站建设分金手指排名一
  • 社区网站制作教程社交媒体营销
  • 云南省建设工程质量监督管理站网站房产网站推广
  • 做网站挣钱吗营销导向的企业网站建设步骤
  • 优化网站 优帮云网站是意识形态建设
  • 网站内容建设运维服务wordpress envato主题
  • 响应式网站的几种尺寸推广网站的几种方法
  • php 打开网站东莞网站建设网页推广
  • 正品海外购网站有哪些公司网站开发费用兴田德润在哪儿
  • 怎样做编辑发到网站自由建网站的网络程序
  • 网站开发 卡片cms建站系统 下载
  • 新浪门户网站是谁做的科技网站建设
  • 免费网站建设合同书山西网站建设企业
  • 网站建设空间什么意思学做网站什么语言合适
  • 网站开发的形式有( )友情链接英文
  • 帝国网站管理系统前台免费photoshop下载
  • 深圳一百讯网站建设wordpress汉化包
  • 建设一个班级网站的具体步骤自己的网站源代码一片空白
  • 初创公司 建网站wordpress 模板获取数据库
  • 怎么在网站做推广不要钱六安网约车平台
  • 申晨推荐的营销网站做卖挖掘机的网站