python脚本里的语句是
from bs4 import BeautifulSoup
import urllib2
response = urllib2.urlopen('http://xxxxxx')
html = response.read()
soup = BeautifulSoup(html)
res = soup.find_all(class_='pro-detail-price')
得到的res是ResultSet类型,print结果是:
[<p class="pro-detail-price">最低价格:¥<span>4399.0</span>(天猫旗舰店)
<p class="pro-detail-trend">
<iframe frameborder="0" height="210" scrolling="no" src="http://tool.manmanbuy.com/historyPro.aspx?w=290&amp;h=210&m=1&e=0&scroll=0&id=327958" width="290"></iframe>
</p>
</p>]
如果想在这个ResultSet里找到特定的标签改用什么办法呢? 比如找到<span>4399.0</span>里的4399.0
PHP中文网2017-04-17 17:11:55
res = soup.find(class_='pro-detail-price').find('span').string
Is this okay?
巴扎黑2017-04-17 17:11:55
I remember that find will only return the first matching result, and findAll will return ResultSet
So the find result should be directly res.span.string, if not, just res[0].span .stringbar
大家讲道理2017-04-17 17:11:55
find_all()
and find()
are not easy to use. The best one for bs is select()
, which is the CSS selector.
You have obtained a large frame, for example, it is a Tag in soup, then: find_all()
和find()
都不好用,bs最好用的是select()
,即CSS选择器。
你已经获取了一个大框架吧,比如它是soup中的一个Tag,然后:
results = tag.select('p[class="pro-detail-price"] span')
你这个例子里,results[0]
rrreee
results[0]
is the specified tag. 🎜大家讲道理2017-04-17 17:11:55
from bs4 import BeautifulSoup
import urllib2
response = urllib2.urlopen('http://xxxxxx')
html = response.read()
soup = BeautifulSoup(html)
res = soup(class_='pro-detail-price')
for sp in res:
print sp.span.string
I tried it randomly, and the output result of changing to this code is 4399.0, but I hope the master can provide a better way, thank you
怪我咯2017-04-17 17:11:55
In the results found by find, you can use the CSS selector select to perform the next positioning search.