BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #5213同步于 2015/2/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

关于BeautifulSoup新手小白求答疑解惑。。。

Dlovingalice
2015/2/20镜像同步7 回复
用Beautiful Soup官方中文文档的写法 soup.find_all("a", class_="sister") 找不到<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>这个东东。。。显示结果是个空list。。。 当然class删了下划线就更没戏了。。。。 但是soup.find_all(attrs={"class":"sister"})是可以找到的(只改了这一行就实现了)。。。不知道是不是我有什么没有import的东西还是什么原因?官方文档的说法为何不行呢? 向诸位大神请教。。。 顺便拜年!!!羊年大吉!!!潜水党默默等回答。。。
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
nuanyangyang机器人#1 · 2015/2/22
【 在 Dlovingalice 的大作中提到: 】 : 用Beautiful Soup官方中文文档的写法 : soup.find_all("a", class_="sister") : 找不到<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>这个东东。。。显示结果是个空list。。。 : ................... 在我这里没问题。
Dlovingalice机器人#2 · 2015/2/23
我擦为什么。。。。T.T
nuanyangyang机器人#3 · 2015/2/23
【 在 Dlovingalice 的大作中提到: 】 : 我擦为什么。。。。T.T 你贴一个完整的可执行的可以重现你的问题的代码来吧。
Dlovingalice机器人#4 · 2015/2/23
from bs4 import BeautifulSoup html = """ <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="brother" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ soup = BeautifulSoup(html) q=soup.find_all("a",class_="brother") print q 以上就是我的代码,不能实现选出brother的功能。 但是下面的代码可以实现: from bs4 import BeautifulSoup html = """ <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="brother" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ soup = BeautifulSoup(html) q=soup.find_all(attrs={"class":"brother"}) print q 【 在 nuanyangyang 的大作中提到: 】 : : 你贴一个完整的可执行的可以重现你的问题的代码来吧。
nuanyangyang机器人#5 · 2015/2/24
【 在 Dlovingalice 的大作中提到: 】 : from bs4 import BeautifulSoup : html = """ : <html><head><title>The Dormouse's story</title></head> : ................... 我这里还是正常。 你用Python3试试看?
nuanyangyang机器人#6 · 2015/2/24
【 在 Dlovingalice 的大作中提到: 】 : from bs4 import BeautifulSoup : html = """ : <html><head><title>The Dormouse's story</title></head> : ................... 另外,你是如何安装的bs4? 我是用pip3 install beautifulsoup4
Dlovingalice机器人#7 · 2015/2/24
我也是这样安装的。。。试一下Python3吧。。。 【 在 nuanyangyang 的大作中提到: 】 : : 另外,你是如何安装的bs4? : 我是用pip3 install beautifulsoup4