鱼C论坛

 找回密码
 立即注册
查看: 1572|回复: 5

关于python爬虫工具Beautifulsoup的问题

[复制链接]
发表于 2017-12-17 22:19:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from bs4 import BeautifulSoup

  2. html = """
  3. <html><head><title>The Dormouse's story</title></head>
  4. <body>
  5. <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
  6. <p class="story">Once upon a time there were three little sisters; and their names were
  7. <a  class="sister" id="link1"><!-- Elsie --></a>,
  8. <a  class="sister" id="link2">Lacie</a> and
  9. <a  class="sister" id="link3">Tillie</a>;
  10. and they lived at the bottom of a well.</p>
  11. <p class="story">...</p>
  12. """
  13. soup = BeautifulSoup(html,'lxml')
  14. print(soup.title)
复制代码


代码如上,运行结果如下:
捕获.PNG

我的问题是,除了最后一行是程序正常的运行结果,前面这些“sax:......”是什么内容?如何才能让运行结果中没有这些内容?
python学习的一枚新手,求教各位。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-17 23:51:27 | 显示全部楼层
安装 lxml4 或者使用 html.parser 来解析
sax是解析xml的一种方式

from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a  class="sister" id="link1"><!-- Elsie --></a>,
<a  class="sister" id="link2">Lacie</a> and
<a  class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html,'html.parser')
print(soup.title)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-18 12:42:09 | 显示全部楼层
鹏程2018 发表于 2017-12-17 23:51
安装 lxml4 或者使用 html.parser 来解析
sax是解析xml的一种方式

我试过了,还是会出现这些内容,我多试了几次发现,这些内容是在import bs4的时候就会输出,请问如何才能在import bs4时设置不输出这些内容?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-18 14:56:20 From FishC Mobile | 显示全部楼层
请问你用的是什么的版本的BeautifulSoup 我用的是4-4.6.0版,是用pip安装的。并没有你说的那个问题!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-18 16:31:23 | 显示全部楼层
鹏程2018 发表于 2017-12-18 14:56
请问你用的是什么的版本的BeautifulSoup 我用的是4-4.6.0版,是用pip安装的。并没有你说的那个问题!

我也是pip安装的4-4.6.0,那可能是我电脑设置的问题......?还是谢谢您啦~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-23 16:21:56 | 显示全部楼层
我也运行了一下代码,没什么问题,也是用pip安装的,输出如下:
  1. <title>The Dormouse's story</title>
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 13:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表