在这里编程新手:)
我想使用BeautifulSoup从网站打印价格。这是我的代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup, SoupStrainer
from urllib2 import urlopen
url = "Some retailer's url"
html = urlopen(url).read()
product = SoupStrainer('span',{'style': 'color:red;'})
soup = BeautifulSoup(html, parse_only=product)
print soup.prettify()
并按以下顺序打印价格:
<span style="color:red;">
180
</span>
<span style="color:red;">
1250
</span>
<span style="color:red;">
380
</span>
我尝试print soup.text.strip()
,但它返回了1801250380
请帮我打印每行的价格:)
非常感谢!
答案 0 :(得分:2)
>>> print "\n".join([p.get_text(strip=True) for p in soup.find_all(product)])
180
1250
380
答案 1 :(得分:2)
这将为您提供转换为整数的字符串列表:
>>> [int(span.text) for span in soup.find_all('span')]
[180, 1250, 380]