首页 > 编程语言 >python爬虫-bs4基础

python爬虫-bs4基础

时间:2023-02-28 20:25:37浏览次数:54  
标签:story title python 爬虫 soup Dormouse bs4 BeautifulSoup print

# 下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

# 使用BeautifulSoup解析这段代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:

from bs4 import BeautifulSoup

# soup = BeautifulSoup(html_doc)
# 如果BeautifulSoup(html_doc)就这样不做任何处理,回报一个 GuessedAtParserWarning 解析器警告
# 接着往下面看,添个解析器就好
soup = BeautifulSoup(html_doc, 'html.parser')

# print(soup.prettify())

#  几个简单的浏览结构化数据的方法:
print(soup.title)
# <title>The Dormouse's story</title>

print(soup.title.name)
# title

print(soup.title.string)
# The Dormouse's story

# 上一级标签名字
print(soup.title.parent.name)
# head

print(soup.p)
# <p class="title"><b>The Dormouse's story</b></p>

print(soup.p['class'])
# ['title']

print(soup.a)
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

print(soup.find_all('a'))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

print(soup.find(id="link3"))
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

print(soup.find('<body>'))

标签:story,title,python,爬虫,soup,Dormouse,bs4,BeautifulSoup,print
From: https://www.cnblogs.com/Wesuiliye/p/17165833.html

相关文章

  • bs4解析入门-湖南农场品价格行情
    importrequestsfrombs4importBeautifulSoupimportcsvurl='https://price.21food.cn/market/174-p1.html'headers={'User-Agent':'Mozilla/5.0(Wind......
  • linux升级python3.6升级3.7
    1.下载到本地并解压wgethttps://www.python.org/ftp/python/3.7.12/Python-3.7.12.tgztar-zvxfPython-3.7.12.tgzcdPython-3.7.122.编译安装./configure--......
  • [oeasy]python0096_游戏娱乐行业_雅达利_米洛华_四人赛马_影视结合游戏
    游戏娱乐行业回忆上次内容游戏机行业从无到有雅达利公司一枝独秀并且带领行业发展起来雅达利公司优秀员工乔布斯在朋友帮助下完成了《pong》Jobs黑了Woz一部分......
  • Python elasticsearch 使用心得
    一、配置python==3.6/3.8#更高版本的elasticsearch会出现doc_type被统一成_doc导致旧版语句报错的情况pipinstallelasticsearch==7.8.0二、连接esfromelastics......
  • [oeasy]python0096_游戏娱乐行业_雅达利_米洛华_四人赛马_影视结合游戏
    游戏娱乐行业回忆上次内容游戏机行业从无到有雅达利公司一枝独秀并且带领行业发展起来雅达利公司优秀员工乔布斯在朋友帮助下完成了《pong》......
  • python函数
    函数会给一段语句块命名,我们可以在任何时候调用它,运行其中的代码它的一班语法:deffun_name(x):函数语句体returnadef:说明这是一个函数,我们定义了一个函数......
  • canoe和python_给CANoe编程上点套路 – CAPLdll
    canoe和python_给CANoe编程上点套路–CAPLdllweixin_39585974于2020-12-2222:19:59发布734收藏8文章标签:canoe和python版权汽车电子攻城狮:“数据处理算法有点复......
  • python 线程池
    importargparseimportosimportimageioimportcv2importnumpyasnpimporttimefromconcurrent.futuresimportThreadPoolExecutor,wait,ALL_COMPLETED,FIRST_......
  • #!/usr/bin/python到底是什么意思
    关于脚本第一行的 #!/usr/bin/python 的解释,相信很多不熟悉Linux系统的同学需要普及这个知识,脚本语言的第一行,只对Linux/Unix用户适用,用来指定本脚本用什么解释器来......
  • 记一次 python+allure 的学习
    1、allure下载地址:https://github.com/allure-framework/allure2/releases 下载完成后,配置window的环境变量,到bin目录配置完成后,cmd命令窗口输入以下命令,检查是否......