python

当前位置:中华考试网 >> python >> python爬虫 >> 文章内容

Python爬虫中BeautifulSoup对象创建方法有哪些?

来源:中华考试网  [2020年11月11日]  【

  首先必须要导入 bs4 库

  from bs4 import BeautifulSoup

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

  html = """

  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

  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.

  ...

责编:fushihao

上一篇:python爬虫中BeautifulSoup有哪些种类?

下一篇: 没有了

  • 会计考试
  • 建筑工程
  • 职业资格
  • 医药考试
  • 外语考试
  • 学历考试