今天看第二章:复杂html解析
上次的学习笔记链接:https://blog.youkuaiyun.com/Nyte2018/article/details/88713447
前两天看了慕课上的html和css入门,对于大致结构有所了解,有助于学习爬虫。
先来看代码1:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
bsObj = BeautifulSoup(html,"html.parser")
nameList = bsObj.findAll("span", {
"class":"green"})
for name in nameList:
print(name.get_text())
代码1的作用是输出http://www.pythonscraping.com/pages/warandpeace.html这个网站上所有人物名称。
先来看一下这个网站的页面和源码:

<html>
<head>
<style>
.green{
color:#55ff55;
}
.red{
color:#ff5555;
}
#text{
width:50%;
}
</style>
</head>
<body>
<h1>War and Peace</h1>
<h2>Chapter 1</<

本文详细介绍了Python的BeautifulSoup库在复杂HTML解析中的应用,包括find()和findAll()方法的使用,以及其他BeautifulSoup对象的介绍,如Tag、NavigableString和Comment。还探讨了导航树的概念,讲解如何通过标签位置查找信息。
最低0.47元/天 解锁文章
4294

被折叠的 条评论
为什么被折叠?



