Python爬虫中如何创建beautifulsoup对象

小编给大家分享一下Python爬虫中如何创建 beautifulsoup 对象,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

乐至ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

首先必须要导入 bs4 库

from bs4 import BeautifulSoup

我们创建一个字符串,后面的例子我们便会用它来演示

html = """
The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were , Lacie and Tillie; and they lived at the bottom of a well.

...

"""

创建 beautifulsoup 对象

soup = BeautifulSoup(html)

另外,我们还可以用本地 HTML 文件来创建对象,例如

soup = BeautifulSoup(open('index.html'))

上面这句代码便是将本地 index.html 文件打开,用它来创建 soup 对象 下面我们来打印一下 soup 对象的内容,格式化输出

print soup.prettify()

输出结果:


 
  
   The Dormouse's story
  
 
 
  
   
    The Dormouse's story
   
  

  

   Once upon a time there were three little sisters; and their names were                 ,         Lacie        and         Tillie        ; and they lived at the bottom of a well.   

  

   ...   

 

看完了这篇文章,相信你对Python爬虫中如何创建 beautifulsoup 对象有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


文章题目:Python爬虫中如何创建beautifulsoup对象
标题URL:http://csruizhi.cn/article/gspppp.html

其他资讯

Copyright © 2007-2024 成都优众联杰科技有限公司 All Rights Reserved 蜀ICP备2024116266号
友情链接: 成都网站制作 定制级高端网站建设 高端定制网站设计 攀枝花网站设计 成都网站设计公司 温江网站设计 网站制作 成都网站建设公司 成都网站设计 成都网站建设公司 成都网站设计 H5网站制作 定制网站设计 企业网站设计 网站设计 公司网站建设 品牌网站建设 企业网站建设公司 网站建设费用 企业网站建设 成都网站制作 成都定制网站建设