Roy Roy - 23 days ago 14
Python Question

Python-Web Scraping. How to get the elements that are highlighted are bold alone?

I am beginner in Web scraping with python. I am trying to web scrape using python - beautiful soup and selenium. My objective is to get the highlighted elements [ In this case , it's the 1200 Sqft and event alone]. This is my HTML Code,

</div>
<section class='space-section'>
<table class='space-features'>
<tbody>
<tr>
<td>
<i class='icon-measuringtape'></i>
<p class='space-feature-name'>1,200 sqft</p>
</td>
<td class='disabled'>
<i class='icon-store'></i>
<p class='space-feature-name'>Retail</p>
</td>
<td class='disabled'>
<i class='icon-restaurant'></i>
<p class='space-feature-name'>Bar &amp; Restaurant</p>
</td>
<td class=''>
<i class='icon-event'></i>
<p class='space-feature-name'>Event</p>
</td>
<td class='disabled'>
<i class='icon-share'></i>
<p class='space-feature-name'>Shop Share</p>
</td>
<td class='disabled'>
<i class='icon-star'></i>
<p class='space-feature-name'>Unique</p>
</td>
</tr>
</tbody>
</table>
</section>


and the URL of my Website is this - https://www.appearhere.co.uk/spaces/north-kensington-upcycling-store-and-cafe

I want my output print statement to be something like this, SQFT - 1200 sqft , Retail - No, Bar and Restaurant - No, Event - Yes, Shop share - No, Unique - No. Can you tell me a solution for this?

Answer

try this code:

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Chrome()
url= "https://www.appearhere.co.uk/spaces/north-kensington-upcycling-store-and-cafe"
driver.maximize_window()
driver.get(url)

content = driver.page_source.encode('utf-8').strip()
soup = BeautifulSoup(content,"html.parser")
space=soup.find("table",{"class":"space-features"})
feature=space.find_all("p")
options=[y.text for y in feature]
disabled=space.find_all("td",{"class":"disabled"})
options1=[y.text.strip() for y in disabled]

for x in options:
    if 'sqft' in x:
        print "SQFT - ",x
    elif x in options1:
            print x , " - No"
    else:
        print x , " - Yes"

This will print:

SQFT -  1,200 sqft
Retail  - No
Bar & Restaurant  - No
Event  - Yes
Shop Share  - No
Unique  - No

Hope this is what you want

Comments