Bootstrap

python网络数据采集学习笔记(二)

今天看第二章:复杂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>

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;