python - BeautifulSoup中的parent使用的一点疑问
高洛峰
高洛峰 2017-04-18 09:19:07
[Python讨论组]

在看《Python网络数据采集》一书,里面有这样一段代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup

html=urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj=BeautifulSoup(html,"html.parser")
print(bsObj.find("img",{"src:":"../img/gifts/img1.jpg"}).parent.previous_siblingget_text())

但是运行后出错,提示:

AttributeError: 'NoneType' object has no attribute 'parent'

爬取得URL对应的网页页面如图所示:

对应的代码如下:

<html>
<head>
<style>
img{
    width:75px;
}
table{
    width:50%;
}
td{
    margin:10px;
    padding:10px;
}
.wrapper{
    width:800px;
}
.excitingNote{
    font-style:italic;
    font-weight:bold;
}
</style>
</head>
<body>
<p id="wrapper">
<img src="../img/gifts/logo.jpg" style="float:left;">
<h1>Totally Normal Gifts</h1>
<p id="content">Here is a collection of totally normal, totally reasonable gifts that your friends are sure to love! Our collection is
hand-curated by well-paid, free-range Tibetan monks.<p>
We haven't figured out how to make online shopping carts yet, but you can send us a check to:<br>
123 Main St.<br>
Abuja, Nigeria
</br>We will then send your totally amazing gift, pronto! Please include an extra $5.00 for gift wrapping.</p>
<table id="giftList">
<tr><th>
Item Title
</th><th>
Description
</th><th>
Cost
</th><th>
Image
</th></tr>

<tr id="gift1" class="gift"><td>
Vegetable Basket
</td><td>
This vegetable basket is the perfect gift for your health conscious (or overweight) friends!
<span class="excitingNote">Now with super-colorful bell peppers!</span>
</td><td>
$15.00
</td><td>
<img src="../img/gifts/img1.jpg">
</td></tr>

<tr id="gift2" class="gift"><td>
Russian Nesting Dolls
</td><td>
Hand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! <span class="excitingNote">8 entire dolls per set! Octuple the presents!</span>
</td><td>
$10,000.52
</td><td>
<img src="../img/gifts/img2.jpg">
</td></tr>

<tr id="gift3" class="gift"><td>
Fish Painting
</td><td>
If something seems fishy about this painting, it's because it's a fish! <span class="excitingNote">Also hand-painted by trained monkeys!</span>
</td><td>
$10,005.00
</td><td>
<img src="../img/gifts/img3.jpg">
</td></tr>

<tr id="gift4" class="gift"><td>
Dead Parrot
</td><td>
This is an ex-parrot! <span class="excitingNote">Or maybe he's only resting?</span>
</td><td>
$0.50
</td><td>
<img src="../img/gifts/img4.jpg">
</td></tr>

<tr id="gift5" class="gift"><td>
Mystery Box
</td><td>
If you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. <span class="excitingNote">Keep your friends guessing!</span>
</td><td>
$1.50
</td><td>
<img src="../img/gifts/img6.jpg">
</td></tr>
</table>
</p>
<p id="footer">
&copy; Totally Normal Gifts, Inc. <br>
+234 (617) 863-0736
</p>

</p>
</body>
</html>

个人感觉书中代码逻辑上没有错误。。。但是为什么最后IDE会提示'NoneType' object呢。。。是find函数使用不对么。。。应该怎么正确写这段代码呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
天蓬老师
from urllib.request import urlopen
from bs4 import BeautifulSoup

html=urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj=BeautifulSoup(html,"html.parser")      
print(bsObj.find("img",{"src":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text())

你的代碼... 也太多錯...

  1. "src:" 應改為 "src"

  2. parent.previous_siblingget_text() 應改為 parent.previous_sibling.get_text()


我回答過的問題: Python-QA

天蓬老师

{"src:":"../img/gifts/img1.jpg"}应该改为{"src":"../img/gifts/img1.jpg"},它找的src属性,没有:这个符号的。还有后面应该是.previous_sibling.get_text()吧!认真点...

高洛峰

previous_siblingget_text中间少.不说
对find,keyword式
bsObj.find("img",src="../img/gifts/img1.jpg").parent.previous_sibling.get_text()
attr式
bsObj.find("img",attrs={"src":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text()

参考: http://beautifulsoup.readthedocs.io/zh_CN/latest/#find

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号