<jupyter_start><jupyter_text>Beautifulsoup: find by CSS class **First we still need to open a [page](https://mofanpy.com/static/scraping/list.html), then we can apply beautifulsoup on this page's html.**<jupyter_code>from bs4 import BeautifulSoup
from urllib.request import urlopen

# if has Chinese, apply decode()
html = urlopen("https://mofanpy.com/static/scraping/list.html").read().decode('utf-8')
print(html)<jupyter_output><!DOCTYPE html>
<html lang="cn">
<head>
	<meta charset="UTF-8">
	<title>爬虫练习 列表 class | 莫烦 Python</title>
	<style>
	.jan {
		background-color: yellow;
	}
	.feb {
		font-size: 25px;
	}
	.month {
		color: red;
	}
	</style>
</head>

<body>

<h1>列表 爬虫练习</h1>

<p>这是一个在 <a href="https://mofanpy.com/" >莫烦 Python</a> 的 <a href="https://mofanpy.com/tutorials/scraping" >爬虫教程</a>
	里无敌简单的网页, 所有的 code 让你一目了然, 清晰无比.</p>

<ul>
	<li class="month">一月</li>
	<ul class="jan">
		<li>一月一号</li>
		<li>一月二号</li>
		<li>一月三号</li>
	</ul>
	<li class="feb month">二月</li>
	<li class="month">三月</li>
	<li class="month">四月</li>
	<li class="month">五月</li>
</ul>

</body>
</html><jupyter_text>**Parsing this html using a method called lxml, create a soup object. Find all "li" tag which has a class=month.**<jupyter_code>soup = BeautifulSoup(html, features='lxml')

# use class to narrow search
month = soup.find_all('li', {"class": "month"})
for m in month:
    print(m.get_text())<jupyter_output>一月
二月
三月
四月
五月<jupyter_text>**Or using some helpful functions to find tags. Access the attribute of found tags using a key just like what you do in a python dictionary.**<jupyter_code>jan = soup.find('ul', {"class": 'jan'})
d_jan = jan.find_all('li')              # use jan as a parent
for d in d_jan:
    print(d.get_text())<jupyter_output>一月一号
一月二号
一月三号
{'fraction_non_alphanumeric': 0.17274800456100342, 'fraction_numerical': 0.003990877993158495, 'mean_word_length': 4.571428571428571, 'pattern_counts': {'":': 2, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '144650', 'license': 'MIT', 'n_tokens_mistral': 787, 'n_tokens_neox': 770, 'n_words': 179, 'path': 'notebook/2-2-beautifulsoup-css.ipynb', 'repo_name': 'VickyMin1994/easy-scraping-tutorial', 'size': '1754'}