Issue
We were scrapping Amazon.in website to retrieve the price of any product. All products are having different value for 'id' attributes in 'span' tag such as;
id = 'priceblock_ourprice', id = 'priceblock_saleprice', and id = 'priceblock_dealprice'.
Our task is to retrieve the price of the products using the find_all(..) method in Beautiful Soup. From our basic knowledge we were able to give only one parameter to the find_all(..) method as shown below:
m = soup1.find_all('span', {'id': 'priceblock_ourprice'})
Is there any way to give multiple parameters to the find_all(..) method using OR condition?
Following are the links with different values of same 'id' attribute:
Thank you for your help!
Solution
I haven't tested this but I believe you can pass a function as an argument to find_all()
so you could try something like:
def check_id(tag):
valid_ids = ['priceblock_ourprice','priceblock_saleprice','priceblock_dealprice']
if tag.has_attr('id'):
return tag['id'] in valid_ids
else:
return False
m = soup1.find_all(check_id)
Answered By - SQLnoob
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.