美文网首页
第二章 复杂HTML解析

第二章 复杂HTML解析

作者: VB过得VB | 来源:发表于2017-02-04 22:12 被阅读39次

1、获取指定标签内容

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
bs0bj = BeautifulSoup(html, 'lxml')
namelist = bs0bj.findAll('span', {'class': 'green'}) # 获取页面所有指定标签
for name in namelist:
    print(name.get_text())

2、处理子标签

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bs0bj = BeautifulSoup(html, 'lxml')

for child in bs0bj.find('table', {'id': 'giftList'}).children:
    print(child)

3、处理兄弟标签

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html, 'lxml')
for sibling in bsObj.find("table", {"id": "giftList"}).tr.next_siblings:
    print(sibling)
# previous_siblings 前一组
# next_siblings 后一组
# previous_sibling前一个
# next_siblings后一个

4、父标签处理

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html, 'lxml')
print(bsObj.find("img", {"src": "../img/gifts/img1.jpg"
                         }).parent.previous_sibling.get_text()
      )
---------------------------------------------------------------------------
#打印输出
$15.00
---------------------------------------------------------------------------
(1) 选择图片标签src="../img/gifts/img1.jpg";
(2) 选择图片标签的父标签(在示例中是<td> 标签);
(3) 选择<td> 标签的前一个兄弟标签previous_sibling(在示例中是包含美元价格的<td>
标签);
(4) 选择标签中的文字,“$15.00”。

5、正则表达式

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html, 'lxml')
images = bsObj.findAll("img", {"src": re.compile(r"../img/gifts/img.*.jpg")})
for image in images:
    print(image["src"])

相关文章

  • 第二章 复杂HTML解析

    1、获取指定标签内容 2、处理子标签 3、处理兄弟标签 4、父标签处理 5、正则表达式

  • 第二章_复杂HTML解析

    已经确定目标内容后,应该怎么做? 再来一碗 BeautifulSoup 通过属性查找标签 CSS 可以让 HTML...

  • 记一次jsoup的使用

    Jsoup是用于解析HTML,就类似XML解析器用于解析XML。 Jsoup它解析HTML成为真实世界的HTML。...

  • lxml的使用方法

    使用lxml解析HTML代码 1.解析html字符串:使用lxml.etree.HTML进行解析,示例代码如下: ...

  • 阻塞解析与阻塞渲染

    Block Parsing: 阻塞解析 script 标签会阻塞 html 页面的解析,阻塞解析:html 页面会...

  • 重绘和重排(回流)

    一、浏览器渲染HTML的步骤 HTML被HTML解析器解析成DOM Tree, CSS则被CSS解析器解析成CSS...

  • 虚拟DOM是啥?

    一、浏览器渲染HTML的步骤 HTML被HTML解析器解析成DOM Tree, CSS则被CSS解析器解析成CSS...

  • 浏览器渲染原理

    HTML 的解析过程 js 的下载和执行会阻塞 HTML 的解析,等 js 执行完继续去解析 HTML 问题1:为...

  • python bs4的坑

    lxm解析html会导致丢失结构 html5lib解析不会丢失结构

  • android利用jsoup抓取数据

    效果图 首先分析html: 添加依赖 将String的url解析成html 解析Html 实体类 JsoupAdp...

网友评论

      本文标题:第二章 复杂HTML解析

      本文链接:https://www.haomeiwen.com/subject/pdjsittx.html