css选择器

css选择器

"""
# css选择器
#######
#1 css选择器
#######
# 重点

# Tag对象.select("css选择器")
#  #ID号
#  .类名
#   div>p:儿子 和div p:子子孙孙
#   找div下最后一个a标签 div a:last-child


# css选择器,xpath选择器会用了,它就是个通行证(所有的都可以不会,会粘贴就行)

# bs4:自己的选择器,css选择器
# lxml:css选择器,xpath选择器
# selenium:自己的选择器,css选择器,xpath选择器
# scrapy框架:自己的选择器,css选择器,xpath选择器
# #select('.article')



#该模块提供了select方法来支持css,详见官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id37
html_doc = """
The Dormouse's story

The Dormouse's story Once upon a time there were three little sisters; and their names were Elsie Lacie and Tillie;

  • Foo
  • Bar
  • Jay
  • Foo

  • Bar
  • Jay
and they lived at the bottom of a well.

...

""" from bs4 import BeautifulSoup soup=BeautifulSoup(html_doc,'lxml') #1、CSS选择器(前端学的css选择) print(soup.p.select('.sister')) print(soup.select('.sister span')) print(soup.select('#link1')) print(soup.select('#link1 span')) print(soup.select('#list-2 .element.xxx')) print(soup.select('#list-2')[0].select('.element')) #可以一直select,但其实没必要,一条select就可以了 # 2、获取属性 print(soup.select('#list-2 h1')[0].attrs) # 3、获取内容 print(soup.select('#list-2 h1')[0].get_text()) """

你可能感兴趣的:(css选择器)