今天看第二章:复杂html解析
上次的学习笔记链接:https://blog.csdn.net/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</h2>