3

programming newbie here :)

I'd like to print the prices from the website using BeautifulSoup. this is my code:

#!/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()

and it prints prices in the following order:

<span style="color:red;">
 180
</span>
<span style="color:red;">
 1250
</span>
<span style="color:red;">
 380
</span>

I tried print soup.text.strip() but it returned 1801250380

Please help me to print the prices per single row :)

Many thanks!

2 Answers 2

2
>>> print "\n".join([p.get_text(strip=True) for p in soup.find_all(product)])
180
1250
380
Sign up to request clarification or add additional context in comments.

Comments

2

This will get you a list of strings converted to integers:

>>> [int(span.text) for span in soup.find_all('span')]
[180, 1250, 380]

2 Comments

This will stop working if the markup changes from span to, let's say, div.
So you are saying that if the code structure changes, the web scraping code needs to be updated? That should go without saying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.