from bs4 import BeautifulSoupfrom urllib.request import urlopen# if has Chinese, apply decode()html =urlopen("https://morvanzhou.github.io/static/scraping/list.html").read().decode('utf-8')print(html)
在 < head> 中,被放在 < style> 里面的都是某些 class 的 CSS 代码.,比如 jan 就是一个 class,jan 这个类掌控了这个类型的背景颜色,所以在 < ul class="jan"> 这里, 这个 ul 的背景颜色就是黄色的,而如果是 month 这个类, 它们的字体颜色就是红色
按 Class 匹配
找所有 class=month 的信息. 并打印出它们的 tag 内文字.
soup =BeautifulSoup(html, features='lxml')# use class to narrow searchmonth = soup.find_all('li', {"class": "month"})for m in month:print(m.get_text())"""一月二月三月四月五月"""