GrozaFry
GrozaFry

Reputation: 51

How do I web scrape an element using its style definitions like padding, font-size etc. in BeautifulSoup

I want to extract a div using its style property padding-left: 16px, something like shown in the following Python code. But apparently it doesn't work. I know how to extract an element using its class, id or tags. Is there a way to do the same using style property?

from bs4 import BeautifulSoup

f = open("C:\Users\admin\Documents\GitHub\RedditCrawler\new.html");
soup = BeautifulSoup(f);
f.close();

hr2 = soup.find('div', style={"padding-left":"16px"});

print(hr2);

Following is the div I'm trying to extract from my html file:

<html>
<div style="padding-left:16px;">This is the deal</div>
</html>

Upvotes: 2

Views: 2873

Answers (1)

KunduK
KunduK

Reputation: 33384

Use CSS selector to get the div element.

soup.select_one('div[style="padding-left:16px;"]')

Code:

from bs4 import BeautifulSoup
html='''<html>
<div style="padding-left:16px;">This is the deal</div>
</html>'''
soup=BeautifulSoup(html,'html.parser')
#To get the element
print(soup.select_one('div[style="padding-left:16px;"]'))
#To get the text
print(soup.select_one('div[style="padding-left:16px;"]').text)
#To get the style value
print(soup.select_one('div[style="padding-left:16px;"]')['style'])

Output:

<div style="padding-left:16px;">This is the deal</div>
This is the deal
padding-left:16px;

Upvotes: 2

Related Questions