Futochan
Futochan

Reputation: 331

Python Web Scraping BeautifulSoup - substring

Second day on web scraping using Python. I am trying to pull a substring within a string. I wrote the following python code using BeautifulSoup:

 containers = page_soup.findAll("li",{"class":"grid-tile "})
 container_test = containers[7]
 product_container = container_test.findAll("div",{"class":"product-
 swatches"})
 product = product_container[0].findAll("li")
 product[0].a.img.get("data-price")

This outputs the following:

'{"saleprice":"$39.90","price":""}'

How do I print out saleprice and price separately? Result should look like:

saleprice = $39.90
price = ""

Upvotes: 1

Views: 246

Answers (2)

jasongdev
jasongdev

Reputation: 11

this link should be able to help Convert a String representation of a Dictionary to a dictionary?

import ast

BSoutput = '{"saleprice":"$39.90","price":""}'
testing =  ast.literal_eval(BSoutput)
saleprice = testing['saleprice'] 
price = testing['price']
print "saleprice = " + saleprice

Upvotes: 0

n1c9
n1c9

Reputation: 2697

Use the json module - specifically, the loads method, which loads JSON-formatted strings common on websites.

string = '{"saleprice":"$39.90","price":""}'
>>> import json
json_data = json.loads(string)
sale_price = json_data['saleprice']
price = json_date['price']
print(saleprice, price)
>>> (u'', u'$39.90')

The u preceding the string indicates that the string is unicode, which is well explained here.

Additionally, you could use ast.literal_eval, as the string is formatted like a normal Python dictionary. That process would be:

import ast
string = '{"saleprice":"$39.90","price":""}'
dict_representation_of_string = ast.literal_eval(string)
print(string.keys())
>>> ['price', 'saleprice']

Upvotes: 1

Related Questions