• 注册
当前位置:1313e > python >正文

python爬虫之BeautifulSoup库程序笔记

# from bs4 import BeautifulSoup
# html = ''
# soup = BeautifulSoup(html,'lxml')   #使用lxml解析器
# print(soup.prettify())  #格式化网页代码,将页面代码补全
# print(soup.title.string) # 输出title的字符型# 标签选择器
# # 选择元素:
# from bs4 import BeautifulSoup
# html = '我是谁

我来自哪里

'
# soup = BeautifulSoup(html,'lxml') # print(soup.prettify()) # print(soup.title) # 打印出title的内容,注意打印出的同时包含title标签 # print(type(soup.title)) # 打印title的类型 # print(soup.head) # 打印soup的内容 ## 获取内容 # from bs4 import BeautifulSoup # html = '我是谁

我来自哪里

'
# soup = BeautifulSoup(html,'lxml') # print(soup.title.name) #获取较近的标签的名称# 获取属性 # from bs4 import BeautifulSoup # html = '我是谁

我来自哪里

'
# soup = BeautifulSoup(html,'lxml') # print(soup.p.attrs['name']) # 方法1:获取属性name的方法 # print(soup.p['name']) #方法2:获取属性name,直接用.标签加中括号的方式# 获取内容 # print(soup.p.string)# 嵌套选择 from bs4 import BeautifulSoup # html = '我是谁

我来自哪里

'
# soup = BeautifulSoup(html,'lxml') # print(soup.head.title.string) # head中嵌套title# 子节点和子孙节点 # from bs4 import BeautifulSoup # html = '我是谁

我来自哪里

'
# soup = BeautifulSoup(html,'lxml') # print(soup.p.contents) # .contents表示子节点的内容,本质是p中的所有子节点的内容 # # print(soup.p.children) # for i,child in enumerate(soup.p.children): # print(i,child) # # 本质是一个迭代器,不断遍历,使用enumerate方法返回节点内容和索引 # 与之相对应获取子孙节点 # print(soup.p.descendants) # for i,child in enumerate(soup.p.descendants) # print(i,child) # 使用descendants方法将子孙节点获取出来# # 父节点和祖先节点 # from bs4 import BeautifulSoup # html = '我是谁

我来自哪里

'
# soup = BeautifulSoup(html,'lxml') # print(soup.p.parent) # 获取父节点 # print(soup.p.parent.string) # print(list(enumerate(soup.p.parents))) ## 获取兄弟节点 # from bs4 import BeautifulSoup # html = '我是谁

我也不知道

我来自哪里

你好

'
# soup = BeautifulSoup(html,'lxml') # print(list(enumerate(soup.p.next_siblingsstring))) # p标签的后一个兄弟标签 # print(list(enumerate(soup.p.previous_siblings))) # p 标签前一个兄弟标签# 标准选择器 # find_all(name,attrs,recurslve,text) # 可以根据标签名属性内容查找文档 # from bs4 import BeautifulSoup # html = '我是谁

  • hello
  • world
  • 我来自哪里

  • 你好
  • 世界
  • '
    # soup = BeautifulSoup(html,'lxml') # print(soup.find_all('h2')) # 返回所有的h2标签 # print(type(soup.find_all('h2')[0])) # print(soup.find_all('h2')[0]) # 打印出第一个h2标签的内容 # 对标签进行遍历 # for h in soup.find_all('h2'): # print(h.find_all('li')) # print(soup.find_all(attrs={'class':'where'})) # 利用attrs传入字典形式的属性和属性值 # # 对于id和class等特殊类型的查找# print(soup.find_all(id='hello')) # 直接利用id查找 # print(soup.find_all(class_='where')) # 利用class_查找# # 根据文本内容进行选择 # print(soup.find_all(text='hello')) # print(soup.find_all(text='你好')) # 返回的是内容,根据text的内容返回内容# find() # 与find_all方法类似,只不过返回的是匹配结果的第一个元素# css选择器 # 通过select()直接传入css选择器 # 类似于web中的class用点表示class,用#表示id,也可以直接将标签直接传入 # print(soup.select('.where')) # 用.表示class,#表示id,可以嵌套,即.where.hello # print(soup.select('#hello')) # from bs4 import BeautifulSoup # html = '我是谁
  • hello
  • world
  • 我来自哪里

  • 你好
  • 世界
  • '
    # soup = BeautifulSoup(html, 'lxml') # # for d in soup.select('div'): # print(d.select('li')) # 也可以使用ul标签来进行迭代,输出li的标签# 获取属性值 # for d in soup.select('div'): # print(d['id'])# 获取内容 # from bs4 import BeautifulSoup # html = '我是谁
  • hello
  • world
  • 我来自哪里

  • 你好
  • 世界
  • '
    # soup = BeautifulSoup(html,'lxml') # for d in soup.select('div'): # print(d.get_text()) # 使用get_text()方法获得div中的内容 #

    本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

    最新评论

    欢迎您发表评论:

    请登录之后再进行评论

    登录